JavaScript Console Object
Console: -
The console is the most important and used object of JavaScript used for the print, debugging the console.
The console is a built-in object in JavaScript used for:
Printing output
Debugging code
Monitoring program execution
console.log(): -
console.log is the most important and widely used method used for the printing the value massage to the console(or terminal in node.js).
console.log("Hi, this is a printing statement.");
Output:-
Hi, this is a printing statement.
let value = 10;
console.log("value: ",value);
output: -
value: 10
console.log() method is used to:
Print the details
print value
debugging
console.error: -
console.error () method is used to show the error in the console. It mostly appear in red color (❌) sign. It show the some error help to identify an error.
console.error("This is an error");
Output: -
❌This is an error
It helps to identify the bug quickly.
console.table() : -
It is used to show the object/Array in the form of table in the console(or in terminal of Node.js).
const student = {
name: "Ranjeet",
class:"2D",
rollNo: 603,
};
console.table(student);
output:-
| (index) | Values |
|---|---|
| name | Ranjeet |
| class | 2D |
| rollNo | 603 |
console.warn():-
This method is call to show the warning to the user to warn him for certain scenario. Typically, it is shown in the yellow colour(⚠️).
console.warn("This is a warning message.");
Output: -
⚠️This is a warning message.
console.info():-
console.info() is used to print or show the information.
console.info("This is an information");
Output:-
This is an information
Similar to console.log() but semantically clearer.
console.group():-
console.group() function is the method use to create certain amount of code in group together. It has two function to group the block of code.
console.group()
console.groupEnd();
console.group("Group started here: - ");
console.log("hi this is Ranjeet");
console.log("hi this is Ranjeet");
console.log("hi this is Ranjeet");
console.log("hi this is Ranjeet");
console.groupEnd();
Output
Group started here: - hi this is Ranjeet hi this is Ranjeet hi this is Ranjeet hi this is Ranjeet
console.time(): -
console.time() shows the the time takes to show the time taken to execute the the the block of code. It measure the execution time of the code.
console.time("Time to start now");
for(let i = 0; i <= 1000; i++){}
console.timeEnd("Time to start now");
Output: -
Time to start now: 0.1 ms
console.count():-
console.count() is the method that show the number of time a block of code executed.
for(let i = 0; i < 5; i++){
console.count();
}
Output:-
default: 0
default: 1
default: 2
default: 3
default: 4
By default if no printing statement is present it show and print default and show counting.
console.count("Loop");
console.count("Loop");
console.count("Loop");
console.count("Loop");
Output : -
Loop: 1
Loop: 2
Loop: 3
Loop: 4
When multiple massage available,
console.count("hi first");
console.count("hi first");
console.count("hi second");
console.count("hi first");
console.count("hi second");
console.count("hi second");
Output: -
hi first: 1
hi first: 2
hi second: 1
hi first: 3
hi second: 2
hi second: 3
Reset Counter
console.countReset() is used to reset the count to 1 and start it again.
console.count("Hi");
console.count("Hi");
console.count("Hi");
console.countReset("Hi");
console.count("Hi");
Output: -
Hi: 1
Hi: 2
Hi: 3
Hi: 1
