📜 ⬆️ ⬇️

5 functions of the Console object that you did not know about

Not everyone knows that console.log() can be used not only for logging, but also for several other useful operations. I chose the 5 most interesting ways to use the Console, suitable for everyday life.

All the described functions work great in Google Chrome 38



console.assert (expression, message)

If the value of the first argument is false, the system will write the message from the second argument. If the statement is true, nothing will be written.
 > console.assert(document.querySelector('body'), "Missing 'body' element") > console.assert(document.querySelector('.foo'), "Missing '.foo' element") [Error] Assertion failed: Missing '.foo' element 

')


console.table (object)

This function displays the provided object or array in the table:

To learn more about console.table() , I recommend reading Marius Schultz ’s article “Advanced JavaScript Debugging with console.table ()”



console.profile (name)

console.profile(name) outputs the processor load to the console. You can use the report name as an argument. Each launch is saved in a separate tab of the drop-down menu. Do not forget to close the report using console.profileEnd() .




console.group (message)

console.group(message) groups all logs after themselves until receiving the console.groupEnd() command. Lists can branch. console.groupCollapsed(message) works in a similar way, the only difference is in the display.




console.time (name)

console.time(name) starts a timer with the name specified in the argument, which counts the time until it is stopped by the command console.timeEnd(name) . Of course, in both functions you need to use the same name.
 > console.time('Saving user') > console.log('User saved') > console.timeEnd('Saving user') Saving user: 2.750ms 

Source: https://habr.com/ru/post/253081/


All Articles