Cover Image for How to use Console in JavaScript
102 views

How to use Console in JavaScript

The JavaScript console object provides methods for logging messages, debugging, and interacting with the browser console. Here are some commonly used methods of the console object:

  1. console.log(): Logs messages to the console.
JavaScript
console.log('Hello, world!');
  1. console.error(): Logs error messages to the console.
JavaScript
console.error('An error occurred.');
  1. console.warn(): Logs warning messages to the console.
JavaScript
console.warn('This is a warning.');
  1. console.info(): Logs informational messages to the console.
JavaScript
console.info('This is an information.');
  1. console.debug(): Logs debug messages to the console. Note that the availability and behavior of this method may vary across different browsers.
JavaScript
console.debug('Debug information.');
  1. console.clear(): Clears the console.
JavaScript
console.clear();
  1. console.table(): Displays tabular data in the console.
JavaScript
var data = [
  { name: 'John', age: 30 },
  { name: 'Jane', age: 25 }
];

console.table(data);
  1. console.dir(): Displays an interactive listing of the properties of an object.
JavaScript
var obj = { name: 'John', age: 30 };

console.dir(obj);

These are just a few examples of the methods available on the console object. The console is a powerful tool for debugging and logging information during JavaScript development. It provides a convenient way to output messages, inspect objects, and track the execution flow of your code.

You can open the browser’s developer console to view the logged messages and interact with the console output. Different browsers may have additional features and methods available on the console object, so it’s recommended to refer to the browser’s documentation for specific details.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS