Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»General Articles»10 Useful JavaScript Console Methods

    10 Useful JavaScript Console Methods

    RahulBy RahulNovember 14, 20203 Mins ReadUpdated:June 9, 2022

    A JavaScript method is a property containing a function definition. A Console method is an object used to access the browser debugging console. With the help of console methods, we can print messages, warnings, and errors to the browser console, which is helpful for debugging purposes.

    The developer console in a web browser is a tool which logs the information associated with a web page, such as JavaScript, network requests, and security errors etc. To open the developer console window on Google Chrome, use the keyboard shortcut Ctrl + Shift + J (on Windows) or Ctrl + Option + J (on macOS).

    The console object provides multiple methods to use. In this tutorial, you will learn uses of JavaScript console methods.

    1. LOG
    2. INFO
    3. WARN
    4. ERROR
    5. ASSERT
    6. COUNT and COUNTRESET
    7. TIME, TIMELOG and TIMEEND
    8. GROUP and GROUPEND
    9. TABLE
    10. CLEAR

    Here are basic details about all the above-listed functions and uses.

    1. LOG

    Use console.log() method to print a message on web console. It can print a string or any number of JavaScript objects. Instead of displaying anything on the browser console, it logs messages to a debugging console.

    1
    2
    console.log("Hello World");
    console.log("Msg", obj1, obj2);

    2. INFO

    The console.info() function is used to display a message to the browser console. This is pretty similar to console.log but developers use it for permanent messages to output to the console in their code.

    1
    console.info("This is an information message");

    3. WARN

    Use console.warn() function to print warning message on browser console. It takes one argument of message to be displayed.

    1
    console.warn("This is a warning");

    You can also send an object to print on console message. For example:

    1
    2
    var myObj = { firstname : "Rahul", lastname : "K" };
    console.warn(myObj);

    4. ERROR

    Use console.error() method to write an error message to the console. It is used for testing and troubleshooting purpose. It taken one argument of any type like String or Object.

    1
    console.error("Your application has a error");

    Alternatively use:

    1
    2
    var myObj = { firstname : "Rahul", lastname : "K" };
    console.error(myObj);

    5. ASSERT

    Use console.assert() method writes a message to the console based on a condition. It print message to console only If the first argument expression evaluates to false.

    1
    console.assert(document.getElementById("name"), "No element found with ID 'name'");

    6. COUNT and COUNTRESET

    The console.count() method is used to write a message on the console as the number of times it is called.

    1
    2
    3
    for (i = 1; i <= 5; i++){
        cosole.count();
    }

    By default it labled message with “default”, But you can change the label by passing it as an argument.

    Use cosole.countreset() method to reset the counter, So you can use this again further.

    1
    2
    3
    4
    for (i = 1; i <= 5; i++){
        cosole.count();
    }
    cosole.countreset();

    7. TIME, TIMELOG and TIMEEND

    Use console.time() function to start a timer to track how long an operation takes. Before starting operating use time function to start timer and use the console.timeEnd() function once the operating finishes. With the help of this you can find the time taken by a specific operation by the application.

    You can also use console.timelog() to print the current time in your code. This will not start or end any timer, only print the current timestamp.

    1
    2
    3
    4
    5
    console.time("answer time");
    //other block of codes
    console.timeLog("answer time");
    //other block of codes
    console.timeEnd("answer time");

    8. GROUP and GROUPEND

    Use the console.group() function to make group of message on the browser console. This will start grouping of message next to it. Then use the console.grouping() function to end a group that started just before.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    console.log("This is the outer level");
    console.group("First group");
    console.log("In the first group");
    console.group("Second group");
    console.log("In the second group under first group");
    console.warn("Still in the second group");
    console.groupEnd();
    console.log("Back to the first group");
    console.groupEnd();
    console.debug("Back to the outer level");

    9. TABLE

    Use console.table() method to print an object in form of table on browser console.

    1
    console.table({'id':1001, 'name':'tecadmin.net'});

    10. CLEAR

    At the end use console.clear() to clear all the message from browser console. This is good to use at start, so it clears the console first before printing other messages.

    1
    console.clear();

    Conclusion

    Hope this tutorial helps you to understand the uses of JavaScript console methods.

    console javascript methods
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow To Install Wine 5.0 on LinuxMint
    Next Article How to Install Anydesk on Debian 9

    Related Posts

    How To Install Docker on Ubuntu 22.04

    3 Mins Read

    Filesystem Hierarchy Structure (FHS) in Linux

    Updated:July 1, 20222 Mins Read

    What is CPU? – Definition, Types and Parts

    3 Mins Read

    How to Install Ionic Framework on Ubuntu 22.04

    3 Mins Read

    What is the /etc/hosts file in Linux

    Updated:June 27, 20222 Mins Read

    Creating DMARC Record for Your Domain

    Updated:June 29, 20223 Mins Read

    2 Comments

    1. Olivier LeDiouris on June 9, 2022 7:49 am

      Cool stuff, thank you for that!
      Some typos though, “console” is sometimes spelled “cosole” in the code sections.

      Reply
      • Rahul on June 9, 2022 8:05 am

        Thanks Olivier, fixed the typos.

        Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How To Install Docker on Ubuntu 22.04
    • How to Install Bower on Ubuntu 22.04 & 20.04
    • How to run “npm start” through Docker
    • Filesystem Hierarchy Structure (FHS) in Linux
    • How to accept user input in Python
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.