CodesCompiler
πŸ”
HTMLCSSJavaScriptPythonSQLPHPSEOPostsBooksKids About Contact πŸš€ Get Started Free

JavaScript - Console Object

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 βˆ’

JAVASCRIPT CODE
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.

JAVASCRIPT CODE
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.

Preview

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.

JAVASCRIPT CODE
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.

Preview

JavaScript console.clear() Method

The console.clear() method clears the console.

Syntax

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

JAVASCRIPT CODE
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.

Preview

The console object methods list

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

MethodMethod Descriptionassert()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.