JavaScript console API

JavaScript console API

ยท

7 min read

Are you someone who uses console.log to debug most of your code?๐Ÿค”

If you said yes, you're in the right spot. By the end of this article, you would have heard about a variety of different console methods that you can use to make debugging the code a little simpler.๐Ÿคฏ

Each developer (probably ๐Ÿ˜œ) uses various console methods for logging or debugging their code. Whether or not you are using console methods, this article will help you understand the JavaScript console API. Make sure you read it right to the finish. ๐Ÿ‘€

๐Ÿ›  Console Object in JavaScript

The console object in JavaScript offers access to the browser debugging console. It is primarily used to debug the code or log something out of the console.

Working can differ from browser to browser, but there is a de facto collection of features that are usually offered.

โš™๏ธ Web Console

It's a tool which is been used to log information associated with the web page you've been working with. It also allows us to communicate with the web page by executing the JavaScript expression in the contents of the page.

๐Ÿ“Œ NOTE: We'll use the Chrome DevTools Console for this article. Check MDN web docs, if you want to know more about Browser Compatibility.

๐Ÿง Different methods associated with Console Object

  1. console.assert(): It will log an error message to the console if the Assertion is false. If the Assertion is valid, nothing will happen.

    Syntax

    console.assert(assertion, obj1 [, obj2, ..., objN]);
    

    Example

    console.assert(1 === 2, {errorMessage: "Values are not same."});
    

    Output

    1.PNG

    Assertion can be any boolean expression.

  2. console.clear(): This method is used to clear the console. The console will be cleared, in the case of Chrome a simple overlayed text will be printed like: Console was cleared while in firefox no message is returned.

    Syntax

    console.clear();
    
  3. console.count(): Log the number of times this line has been called with the given label.

    Syntax

    console.count([label]); // You can pass any label or else it will take it as default.
    

    Example

    console.count(); // it will be counted as default
    function greet(msg) {
       console.count(msg);
       return msg
    }
    greet('hi');
    greet('hello');
    console.count('hello');
    

    Output

    2.PNG

    In short, console.count() will count the number of times this statement will be called with the label that is been passed.

  4. console.error(): Used for logging console error messages. Useful for debugging the code. The error message will be highlighted with a red color by default.

    Syntax

    console.error(message);
    

    Example

    console.error('LOL: You really screwed up this time.๐Ÿ˜‚')
    

    Output

    3.PNG

  5. console.group() and console.groupEnd(): These methods allow us to group different console statements in a separate block, which will be indented.

    Syntax

    console.group([label]);
    console.groupEnd();
    

    Example

    console.group('The outter level'); 
    console.warn('warning!'); 
    console.error('error occured'); 
    console.log('This is the end for this scope.'); 
    console.groupEnd(); 
    console.log('new section');
    

    Output

    4.PNG

  6. console.log(): This method is used to log the output to the console. We can put any type inside the log(), be it a string, array, object, boolean, etc.

    Syntax

    console.log(param);
    

    Example

    console.log("hello", [1,2,3], { firstName: "Darsh", lastName: "Shah" }, true);
    

    Output

    5.PNG

  7. console.table(): This method allows us to generate a table inside a console. The input must be an array or an object which will be shown as a table.

    Syntax

    console.table(data); // data must be of type array or object.
    

    Example

    const first = ["hi", "hello"];
    const second = { firstName: "Darsh", lastName: "Shah" };
    console.table(first);
    console.table(second);
    

    Output

    6.PNG

  8. console.time() and console.timeEnd(): Whenever we want to know the amount of time spent by a specific block of code, we can use the time() and timeEnd() methods given by the javascript console object. They take a label that must be the same and the code inside may be anything (function, object, specific console, anything).

    Syntax

    console.time(label);
    // Your code goes here.
    console.timeEnd(label);
    

    Example

    console.time('execution'); 
    let fun = function(){ 
     console.log('fun is running'); 
    } 
    let fun2 = function(){ 
     console.log('fun2 is running..'); 
    } 
    fun(); // calling fun(); 
    fun2(); // calling fun2(); 
    console.timeEnd('execution');
    

    Output

    7.PNG

  9. console.trace(): This method outputs the stack trace to the Web Console.

    Syntax

    console.trace();
    

    Example

    function foo() {
       function bar() {
            console.trace();
       }
       bar();
    }
    foo();
    

    Output

    8.PNG

  10. console.warn(): This method is used to log warning message to the console. By default, the warning message will be highlighted with yellow color.

    Syntax

    console.warn(msg);
    

    Example

    console.warn('This is a Warning!');
    

    Output

    9.PNG

Woo-Hoo! You did it! ๐ŸŽŠ Now, you'll be able to make use of all these various console methods, which will in turn simplify the debugging portion for your application.

Thanks, for reading it till the end. ๐Ÿ™


Hope you find that helpful! Let me know your thoughts on this in the comments section. Don't forget to share this article with your friends or colleagues. Feel free to connect with me on any of the platforms below! ๐Ÿš€

Twitter | LinkedIn | GitHub


References:

  1. developer.mozilla.org/en-US/docs/Web/API/co..
  2. 2ality by Dr. Axel Rauschmayer
  3. digitalocean.com/community/tutorials/js-con..
  4. digitalocean.com/community/tutorials/how-to..
  5. Image Source: unsplash.com/photos/95YRwf6CNw8