@ -72,8 +74,15 @@ console.log('%d %s of JavaScript', 30, 'Days')
We can style logging message using css. Copy the following code and paste it on browser console to see the result.
```js
console.log('%c30 Days Of JavaScript','color:green') // log output is green
console.log('%c30 Days%c %cOf%c %cJavaScript%c','color:green','', 'color:red', '', 'color:yellow') // log output green red and yellow text
console.log('%c30 Days Of JavaScript', 'color:green') // log output is green
console.log(
'%c30 Days%c %cOf%c %cJavaScript%c',
'color:green',
'',
'color:red',
'',
'color:yellow'
) // log output green red and yellow text
```
### console.warn()
@ -82,7 +91,9 @@ We use console.warn() to give warning on browser. For instance to inform or war
```js
console.warn('This is a warning')
console.warn('You are using React. Do not touch the DOM. Virtual DOM will take care of handling the DOM!')
console.warn(
'You are using React. Do not touch the DOM. Virtual DOM will take care of handling the DOM!'
)
console.warn('Warning is different from error')
```
@ -110,11 +121,11 @@ Let us also check the result of an object. This creates table with two columns:a
```js
const user = {
name:'Asabeneh',
title:'Programmer',
country:'Finland',
city:'Helsinki',
age:250
name:'Asabeneh',
title:'Programmer',
country:'Finland',
city:'Helsinki',
age:250
}
console.table(user)
```
@ -122,65 +133,73 @@ console.table(user)
Check the rest of the examples by copying and paste on the browser console.
```js
const countries = [['Finland', 'Helsinki'], ['Sweden', 'Stockholm'], ['Norway', 'Oslo']]
const countries = [
['Finland', 'Helsinki'],
['Sweden', 'Stockholm'],
['Norway', 'Oslo']
]
console.table(countries)
```
```js
const users = [
{
name:'Asabeneh',
title:'Programmer',
country:'Finland',
city:'Helsinki',
age:250
},
{
name:'Eyob',
title:'Teacher',
country:'Sweden',
city:'London',
age:25
},
{
name:'Asab',
title:'Instructor',
country:'Norway',
city:'Oslo',
age:22
},
{
name:'Matias',
title:'Developer',
country:'Denmark',
city:'Copenhagen',
age:28
}
{
name:'Asabeneh',
title:'Programmer',
country:'Finland',
city:'Helsinki',
age:250
},
{
name:'Eyob',
title:'Teacher',
country:'Sweden',
city:'London',
age:25
},
{
name:'Asab',
title:'Instructor',
country:'Norway',
city:'Oslo',
age:22
},
{
name:'Matias',
title:'Developer',
country:'Denmark',
city:'Copenhagen',
age:28
}
]
console.table(users)
```
### console.time()
Starts a timer you can use to track how long an operation takes. You give each timer a unique name, and may have up to 10,000 timers running on a given page. When you call console.timeEnd() with the same name, the browser will output the time, in milliseconds, that elapsed since the timer was started.
```js
const countries = [['Finland', 'Helsinki'], ['Sweden', 'Stockholm'], ['Norway', 'Oslo']]
const countries = [
['Finland', 'Helsinki'],
['Sweden', 'Stockholm'],
['Norway', 'Oslo']
]
console.time('Regular for loop')
for(let i = 0; i <countries.length;i++){
for(let i = 0; i <countries.length;i++){
console.log(countries[i][0], countries[i][1])
}
console.timeEnd('Regular for loop')
console.time('for of loop')
for(const [name, city] of countries) {
for(const [name, city] of countries) {
console.log(name, city)
}
console.timeEnd('for of loop')
console.time('forEach loop')
countries.forEach(([name, city])=> {
countries.forEach(([name, city])=> {
console.log(name, city)
})
console.timeEnd('forEach loop')
@ -222,9 +241,9 @@ console.assert(4 > 3, '4 is greater than 3') // no result
console.assert(3 > 4, '3 is not greater than 4') // Assertion failed: 3 is not greater than 4
for (let i = 0; i <= 10; i += 1) {
let errorMessage = `${i} is not even`;
console.log('the # is ' + i);
console.assert(i % 2 === 0, {number: i, errorMessage: errorMessage})
let errorMessage = `${i} is not even`
console.log('the # is ' + i)
console.assert(i % 2 === 0, {number: i, errorMessage: errorMessage})
}
```
@ -234,43 +253,47 @@ The console.group() can help to group different log groups. Copy the following c