Console Logging

The console object is global and provides access to the browser’s debugging console. It is used to display messages in the browser’s console (open the browser’s developer tools by pressing F12 to see it). On the server side, the same object is available in Node.js. It is used to display messages in the terminal.

There is no specification for how console.* methods behave in ECMAScript. Each runtime (browser, Node.js, etc.) adds its own methods and behavior. However, almost all environments support the following:

  • Logging information such as printing out a message or the current value of a variable

    console.log("This is my message");
    
  • Sending a warning to the console

    console.warn("Resource not changed");
    
  • Sending an error message to the console

    console.error("File not found!");
    

The difference between the three is usually the color of the message and the output stream it goes to. In many browsers, console.log is black, console.warn is yellow, and console.error is red. In Node.js, warnings and errors often go to stderr, and colors can vary.

There are other methods available in the console object, including console.table, console.time, console.timeEnd, console.group, console.groupEnd, console.clear, and more. You can find more information about them in the MDN documentation.