JavaScript Console Object and Methods

In this JavaScript tutorial, we are going to learn about the JavaScript Console Object and the different methods used with console object. Use these console methods in your javascript code or on the browser's console to see the output:


What is JavaScript Console Object?

  • JavaScript console object provides access to the browser's debugging console.
  • It is the property of the window object.

  • We can access the console object using window.console or just console.
  • In JavaScript console object is a built-in object that provides developers with a way to interact with the browser's console or terminal.
  • It is commonly used for debugging and testing JavaScript code, but it can also be used to display information to the user.


List of JavaScript Console Methods

Here are some commonly used methods of the console object:

1). console.log();

  • This method is used to print a message on the web browser's console.
  • This method can print strings, numbers, arrays, and objects.

console.log('Hey developer visit geekshelp.in');


2). console.info();

  • It is used to display a message to the browser console.
  • This is very similar to console.log(); but it is used by developers for permanent messages to output to the console in their code.

console.info('This is an information message');


3). console.warn();

  • This method is used to print the warning message on the browser console.
  • This method takes a single argument of a message to be displayed on the browser's console.

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


4). console.error();

  • This console method is used to print an error message in the console.
  • It is mainly used for testing and troubleshooting purposes when we want to display any errors.
  • It takes a single argument of any type like String or Object.

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


5). console.count();

  • This method is used to print a message on the console as the number of times it is called.
  • Or It counts the number of every count and prints it on the browser console.

for (a = 1; a <= 5; a++) {
    console.count();
}


6). console.assert();

  • This console method is used to print messages on the browser's console based on the conditions.
  • It prints the message on the console when the assertion is failed. And if the assertion is true then nothing happens.

let a = 20;
let b = 3;
console.assert(b>a);


7). Time, Timelog and TimeEnd

  • This method is used to start a timer to track the logs in the browser.
  • console.time(); method is used to start the timer and console.timeEnd(); to end the timer or to stop the timer.
  • And the console.timeLog(); is used to log the current value of the timer.

console.time("answer time"); //other block of codes

console.timeLog("answer time"); //other block of codes

console.timeEnd("answer time");


8). Group and GroupEnd

  • This method is used to make a group of messages on the console of the web browser.
  • And the console.groupEnd(); the method is used to end a message group.

console.log("Mern Stack Developer RoadMap");
// First Group Started
console.group("Frontend Part");
console.log("Learn HTML");
console.log("Learn CSS");
console.log("Learn JavaScript");
console.groupEnd();
// First Group Ended

// Second Group started
console.group("Backend Part");
console.log("Learn NodeJS");
console.log("Learn ExpressJS");
console.log("Learn MongoDB");
console.groupEnd();
// Second Group Ended


9). console.table();

  • This method is used to print any object in the form of a table in the console of a web browser.
  • To print the object in the form of a table you can use the code given below.

console.table({'name': 'raju webdev', 'role': 'Frontend Developer'});


10). console.clear();

  • This console method is used to clear the messages from the console.
  • This method will clear all the previous logs.

console.clear();


11). console.debug();

  • This method outputs a message to the console with the log-level debug.

console.debug('This is debug');

Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Top Post Ad

Below Post Ad

Ads Section