DEV Community

Cover image for lets-have-fun-with-console-in-javascript ❤
aryan015
aryan015

Posted on • Edited on

lets-have-fun-with-console-in-javascript ❤

console.table

const users = [
    {id:1,name:'WDE'},
    {id:2}
]
console.log(users)
Enter fullscreen mode Exit fullscreen mode

output
|(index)|id|name|
|--|--|--|
|0|1|WDE|
|1|2||

console.time

estimate time complexity of a program in ms🤣

console.log('fetching')
fetch('url').then(()=>{
    //awaiting response
    console.timeEnd('fetched') //fetched:0ms
})
Enter fullscreen mode Exit fullscreen mode

console.dir

will display all property related to that datatype

const obj = {
    name:'aryan',
    age:26
}
console.dir(obj);
Enter fullscreen mode Exit fullscreen mode

output1

Object {name:'aryan',age:21}
Enter fullscreen mode Exit fullscreen mode

output2

Image description

console.trace

It will display call stack in decending order. last executed to firstly executed.

function foo(){
    function boo(){
        //do something
        console.trace() // start tracing
    }
    boo();
}
// boo
// foo
// anonymous
Enter fullscreen mode Exit fullscreen mode

console.assert

The console.assert() static method writes an error message to the console if the assertion is false. If the assertion is true, nothing happens.

console.log(condition,error)
Enter fullscreen mode Exit fullscreen mode
console.assert(0===1,'not true')
Enter fullscreen mode Exit fullscreen mode

Image description

ps: i dnot know use of this🤣

console.count

function fun(x){
    console.count(x);
}
//count the number of fun runs
fun('hi')
fun('hi')
fun('hi')
fun('hi')
Enter fullscreen mode Exit fullscreen mode

output

'hi': 4
Enter fullscreen mode Exit fullscreen mode

clean the console.clear() 🤣

console.log('pre written message')
console.clear() // hey javascript clear all prev message
console.log('new messages aryan')
Enter fullscreen mode Exit fullscreen mode

list in console.group()

output

do this
    step1:follow
    step2:like
Enter fullscreen mode Exit fullscreen mode

code

console.group('do this')
console.log('setp1:follow')
console.log('setp2:like')
console.groupEnd() //reset
Enter fullscreen mode Exit fullscreen mode

post-credit:@code4git

learning resources

🧡Scaler - India's Leading E-learning
🧡w3schools - for web developers

Top comments (0)