What is the Console in JavaScript. — Learn With Shikha
A web console is a tool which allow us to interact with web page by executing JavaScript expression in the content of the page. Which used to log information associated with a web page like network request, JavaScript security error etc.
In the JavaScript , the console object provide access to the browser’s debugging console. To open the console in the web browser we use Ctrl + Shift+ i for windows and Command+Option+k for Mac. Also with right click and select the inspect as shown in the image below.
Console object methods
Console object in JavaScript provide us several methods. As shown below.
console.log()
The console.log() method is used to writes a message to the console. We can write any think inside the log(), it can be string, array, etc.
Syntax
console.log(message)
For example:
<script> var arr["gold", "selever"]; console.log(arr); console.log("hello"); </script>
As a result:
console.error()
The console.error() method is used to write an error message to the console. By default the error message will be highlighted with red color.
Syntax
console.error("This is a error message");
As a result
console.warn()
The console.warn() method writes a warring to the console. By default the warning message will be highlighted with yellow color.
Syntax
console.warn("message");
Example
<script> console.warn("This is a warnning"); </script>
As a result
Console.clear();
syntax
console.clear();
Example
console.clear();
as a result
console.count()
this method count the number of that the function hit by this counting method. You can add a label that will be included in the console view. By default the label “default” will be added.
syntax
console.count(label)
Example
for(let i=0;i<5;i++){ console.count(i); }
As a result
console.group()
This method indicates the start of a message group. All message will from now on be written inside this group.
syntax
console.group(label)
Example
console.log("Hello world!"); console.group("myLabel"); console.log("Hello again, this time inside a group, with a label!");
as a result
console.groupEnd()
The console.groupEnd method is a indicates the end of a message group
Syntax
console.groupEnd()
Example
console.log("Hello world!"); console.group(); console.log("Hello again, this time inside a group!"); console.groupEnd(); console.log("and we are back.");
As a result
console.table()
The console.table() method allow us to write a table in the console view. The first parameter is required, and must be either an object or an array, containing data to fill the table.
Syntax
console.table(tabledata, tablecolumns)
Example
console.table({ DogName: "Rio", color: "White" });
As a result
console.time()
This console.time() ]method allow us to time certain operations in our code for testing purposes. This method starts a time in the console view.
Syntax
console.time(label)
Example
console.time("test1"); for (i = 0; i < 100000; i++) { // some code } console.timeEnd("test1");
As a result
console.timeEnd()
The console.timeEnd() method ends a timer, and writes the result in the console view. This method allows you to time certain operations in your code for testing purposes. Use the console.time() method to start the timer. Use the label parameter to specify which timer to end.
Syntax
console.timeEnd(label)
example
console.time("test1"); for (i = 0; i < 100000; i++) { // some code } console.timeEnd("test1");
As a result
Recommended Posts:
Difference between the Java and JavaScript
Implementation of Array class in JavaScript jQuery Introduction
Reference
Originally published at https://learnwithshikha.com on January 1, 2021.