What is the Console in JavaScript.

A web console is a tool that allows us to interact with a web page by executing JavaScript expression in the content of the page. That we used to log information associated with a web page like network requests, JavaScript security errors, etc.

In JavaScript, the console object provides access to the browser’s debugging console. So 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 listed below.

  • log()
  • error()
  • warn()
  • clear()
  • count()
  • group()
  • groupEnd()
  • table()
  • time()
  • timeEnd()

console.log()

The console.log() method apply to writes a message to the console. We can write anything inside the log(), which 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 applies to write an error message to the console. By default, the error message will appear in red.

Syntax


console.error("This is a error message");

As a result

console.warn()

The console.warn() method writes a warning to the console. By default, the warning message will appear in yellow.

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 counts 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 messages 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 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 allows 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 allows us to time certain operations in our code for testing purposes. This method starts at 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:

What is JavaScript?

Difference between the Java and JavaScript

Implementation of Array class in JavaScript

jQuery Introduction

Reference

geeksforgeeks.org

w3schools.com

Leave a Reply

Your email address will not be published. Required fields are marked *