Course
Console Object
JavaScript Tutorial
This JavaScript tutorial is crafted for beginners to introduce them to the basics and advanced concepts of JavaScript. By the end of this guide, you'll reach a proficiency level that sets the stage for further growth. Aimed at empowering you to progress towards becoming a world-class software developer, this tutorial paves the way for a successful career in web development and beyond.
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.