These are 11 useful JavaScript console
commands to know about.
1. console.log()
The first and most common command is the console.log() command. It takes in a message and prints out the result to the console. It can also print JavaScript objects, arrays - all data types in JavaScript. Additionally, it takes care of formatting the output of the result to make it easier to trace.
console.log("Hello World"); // Hello World
console.log({
name: "Sam Simon",
age: 43,
});
console.log(["Apple", "Banana", "Orange"]);
2. console.error()
The console.log() command is used by developers for most of the things they do - including logging errors to the console. But do you know there is a special console command for that ? It is the console.error()
command. It is very similar to the console.log() command except it wraps what you log in a red error box.
console.error("Something is wrong");
3. console.info()
This console command is especial useful to output information to the console. Rather than using console.log(), you will use console.info() to make the information stand out from other console commands.
console.info("You are awesome");
4. console.table()
When dealing with arrays, you usually like to represent it in an easy to understand structure. The console.table() commands handles that for you.
console.table(["orange", "apple", "grape"]);
5. console.assert()
This console command writes an error message to the console if an evaluates condition is false.
console.assert(2 > 3, "It cannot be");
6. console.clear()
This command clears the console for you.
console.clear(); //clears the console
7. & 8. console.group()
and console.groupEnd()
These two console commands are useful to group stuff together in the console.
The console.group() is used to start a group. The group will continue until it encounters a console.groupEnd().
9. console.warn()
Logging warnings in the console should be easy! Thats why the console.warn command exists
console.warn("Some warning");
10. & 11. console.time()
and console.timeEnd()
There will be times when you will ned to measrue the time taken for an operation to complete. For this situations, you can make use of teh console.time() and console.timeEnd() functions. You use the console.time() to start a timer and console.timeEnd() to stop the timer.
console.time();
for (let i = 0; i < 4; i++) {
console.log("number " + i);
}
console.timeEnd(); // prints time taken since the timer started
You have reached the end. If you enjoyed this post, please share. I encourage you to try ou these console commands to understand them best. Thanks XD
This post was originally written on me new blog