Console in JavaScript

Console in JavaScript

A Console is an object that provides access to the browser debugging console. The console object can be accessed from a global object. You can view the console with the F12 key. Let’s view some Console techniques used in JavaScript:

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

1. log()

The console log outputs a message to the browser web console. The console() is generally used for testing purposes. It supports all major browsers. The console accepts a single parameter.

console.log("Message to the console");

Output :

Console log | Console in JavaScript

2. warn()

The console.warn() method writes a warning to the console. This parameter is used to hold the warning message.

console.warm("I warn you !!");
Console warn

3. error()

The console.error() method writes an error message to the console. This console is useful for testing purposes.

console.error("Printing an error");
Console error

4. clear()

The console.clear() method clears the console. It clears the console but writes a message “Console was cleared”.

console.clear();
Clear

5. count()

Logs the number of times that this particular call to count() has been called⁣. You can add a label that will be included in the console view. By default the label “default” will be added.

for (let i = 0; i < 5; i++) {
    console.count(i);
}
Count

6. table()

The console.table() method writes 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.

console.table ({
    name: 'John',
    id: 69,
    skills: ['JavaScript', 'React', 'CSS']
});
table

7. time() & timeEnd()

The console.time() method starts a timer in the console view. This method allows you to time certain operations in your code for testing purposes. Use the label parameter to specify which timer to end.

console.time("test");
for(i = 0; i < 1000; i++) {
  document.write = i;
}
console.timeEnd("test");
time

Share this post, if you have liked the blog & thanks for scrolling ❤ and You can read our other blogs here. Follow us on Instagram & GitHub.

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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

thirteen − ten =