• JavaScript Video Tutorials

JavaScript - Console Object



Window Console Object

In JavaScript, the 'console' object is a property of the window object. It allows the developers to access the debugging console of the browser.

The console object contains the various methods used for different functionalities. In Node.js, the console object is used to interact with the terminal.

We access the console object using the window object or without using the window object - window.console or just console.

Console Object Methods

There are many methods available on the console object. These methods are used to perform a number of task such testing, debugging and logging.

You may access the 'console' object methods using the following syntax −

window.console.methodName();
OR
console.methodName();

You can observe outputs in the console. To open the console, use the Ctrl + shift + I or Cmd + shift + I key.

Below, we will cover some methods of the console object with examples.

JavaScript console.log() Method

You can use the console.log() method to print the message in the debugging console. It takes the expression or text message as an argument.

Syntax

Follow the syntax below to use the console.log() method.

console.log(expression);

In the above syntax, the expression can be a variable, mathematical expression, string, etc., which you need to print in the console.

Example

In the below code, clicking the button will invoke the 'printMessage' function. The function prints the string text and number value in the console.

<html>
<body>
   <h2> JavaScript console.log() Method </h2>
   <button onclick = "printMessage()"> Print Message in Console </button>
   <p> Please open the console before you click "Print Message in Console" button</p> 
   <script>
      function printMessage() {
         console.log("You have printed message in console!");
         let num = 10;
         console.log(num);
      }
   </script>
</body>
</html>

JavaScript console.error() Method

The console.error() method prints the error message in the console, highlighting the error with a red background.

Syntax

Follow the syntax below to use the console.error() method.

console.error(message);

The console.error() message takes a message as an argument.

Example

In the below code, printError() function logs the error in the console when you click the button. You can observe the error highlighted with the red background.

<html>
<body>
   <h2> JavaScript console.error() Method </h2>
   <button onclick="printError()"> Print Error Message in Console </button>
   <p> Please open the console before you click " Print Error Message in Console" button.</p> 
   <script>
      function printError() {
         console.error("Error occured in the code!");
      }
   </script>
</body>
</html>

JavaScript console.clear() Method

The console.clear() method clears the console.

Syntax

Follow the syntax below to use the console.clear() method.

console.clear()

Example

In the below code, we print messages to the console. After that, when you click the button, it executes the clearConsole() function and clears the console using the console.clear() method.

<html>
<body>
   <h2> JavaScript console.clear() Method </h2>
   <button onclick = "clearConsole()"> Clear Console </button>
   <p> Please open the console before you click "Clear Console" button</p> 
   <script>
      console.log("Hello world!");
      console.log("Click the button to clear the console.");
      function clearConsole() {
         console.clear();
      }
   </script>
</body>
</html>

The console object methods list

Here, we have listed all methods of the console object.

Method Method Description
assert()It prints the error message in the console if the assertion passed as a parameter is false.
clear()It clears the console.
count()It is used to count how many times the count() method is invoked at a specific location.
error()It displays the error message in the console.
group()It is used to create a group of messages in the console.
groupCollapsed()It is used to create a new collapsed group of messages in the console.
groupEnd()It is used to end the group.
info()It shows the informational or important message in the console.
log()It prints the message into the output.
table()It shows the data in the tabular format in the console.
time()It is used to start the time in the console.
timeEnd()It stops the timer started by the time() method.
trace()It displays the stack trace in the console.
warn()It shows the warning message in the console.
Advertisements