DEV Community

Cover image for Top JS console methods to know as a beginner!
Shivanshu Pareek
Shivanshu Pareek

Posted on

Top JS console methods to know as a beginner!

Hey guys, I just wanted to share some important JS console methods which helped be as a beginner in Javascript. Hope you find it helpful!


The Console API provides several methods to output messages, errors, and other information to the console. Here are some common methods you can use in the console:

console.log()

Outputs a message to the console. You can pass one or more arguments, which will be concatenated with a space in between.

Example:
console.log('Hello, world!');
Enter fullscreen mode Exit fullscreen mode

console.error()

Outputs an error message to the console. Similar to console.log(), but with a red colour and an "Error" prefix.

Example:
console.error('Something went wrong!');
console.error(new Error('Invalid input'));
Enter fullscreen mode Exit fullscreen mode

console.warn()

Outputs a warning message to the console. Similar to console.log(), but with a yellow colour and a "Warning" prefix.

Example:
console.warn('Deprecated function used!');
console.warn('Please update your code');
Enter fullscreen mode Exit fullscreen mode

console.info()

Outputs an informational message to the console. Similar to console.log(), but with a blue colour and an "Info" prefix.

Example:
console.info('Application started');
console.info('Connected to database');
Enter fullscreen mode Exit fullscreen mode

console.debug()

Outputs a debug message to the console. Similar to console.log(), but with a grey colour and a "Debug" prefix.

Note that this method is only available in some browsers and Node.js environments.

Example:
console.debug('Entering function foo()');
console.debug('Variable x has value:', x);
Enter fullscreen mode Exit fullscreen mode

console.assert()

Outputs an error message to the console if the first argument is false. Useful for debugging and testing.

Example:
console.assert(typeof x === 'number', 'x must be a number');
console.assert(y > 0, 'y must be positive');
Enter fullscreen mode Exit fullscreen mode

console.table()

Outputs a table with the provided data. In the below given example it will output the index, name & runs

Example:
const data = [
  { name: 'John', age: 30 },
  { name: 'Jane', age: 25 },
  { name: 'Bob', age: 40 },
];
console.table(data);
Enter fullscreen mode Exit fullscreen mode

console.clear()

Clears the console.

Example:
console.clear();
Enter fullscreen mode Exit fullscreen mode

console.count()

Outputs the number of times the console.count() method has been called with the same label.

Example:
console.count('loop iteration');
console.count('loop iteration');
console.count('another label');
Enter fullscreen mode Exit fullscreen mode

console.group() & console.groupEnd()

The console.group() outputs a group set of console messages together, making it easier to read and debug.
The console.groupEnd() outputs the end of group set of console messages.

Example:
console.group('My group');
console.log('Message 1');
console.log('Message 2');
console.groupEnd();
Enter fullscreen mode Exit fullscreen mode

console.time() & console.timeEnd()

Measures the time it takes to execute a block of code.

Note that console.timeEnd() is necessary since the measurement will not work without a start timer and a stop timer.

Example:
console.time('myTimer');
// some code here
console.timeEnd('myTimer');
Enter fullscreen mode Exit fullscreen mode

console.trace()

Outputs a stack trace to the console

Example:
console.trace();
Enter fullscreen mode Exit fullscreen mode

These are the most commonly used console methods. There are a few more, like console.dir() and console.dirxml(), but they're less frequently used.

Remember, the console is a powerful tool for debugging and testing your code. Use it wisely!

Thank you for your time, hope this was useful!

Top comments (0)