Skip to content

Latest commit

 

History

History
53 lines (38 loc) · 1.59 KB

File metadata and controls

53 lines (38 loc) · 1.59 KB

JavaScript console

📖 Deeper dive reading: MDN JavaScript Console

The JavaScript console object provides interaction with the JavaScript runtime's debugger console. This usage of console should not be confused with your operating system's console (AKA terminal or command line). The console object provides functionality for outputting the value of text and objects, running timers, and counting iterations. These are useful debugging tools when you can actually execute your code in an interactive debugger (such as VS Code).

Log

The basic usage of the console object is to output a log message.

console.log('hello');
// OUTPUT: hello

You can create formatted messages in the log parameter.

console.log('hello %s', 'world');
// OUTPUT: hello world

You can even specify CSS declarations in order to style the log output.

console.log('%c JavaScript Demo', 'font-size:1.5em; color:green;');
// OUTPUT: JavaScript Demo //in large green text

Timers

If you are trying to see how long a piece of code is running you can wrap it with time and timeEnd calls and it will output the duration between the time and timeEnd calls.

console.time('demo time');
for (let i = 0; i < 10000000; i++) {}
// ... some code that takes a long time.
console.timeEnd('demo time');
// OUTPUT: demo time: 12.74 ms

Count

To see how many times a block of code is called you can use the count function.

console.count('a');
// OUTPUT: a: 1
console.count('a');
// OUTPUT: a: 2
console.count('b');
// OUTPUT: b: 1