11 JavaScript Console Commands Explained

josiasaurel

Josias Aurel

Posted on December 3, 2020

11 JavaScript Console Commands Explained

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"]);


Enter fullscreen mode Exit fullscreen mode

console.log objects

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");


Enter fullscreen mode Exit fullscreen mode

console.error example

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");


Enter fullscreen mode Exit fullscreen mode

console info example

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"]);


Enter fullscreen mode Exit fullscreen mode

console.table() example

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");


Enter fullscreen mode Exit fullscreen mode

console.assert example

6. console.clear()

This command clears the console for you.



console.clear(); //clears the console


Enter fullscreen mode Exit fullscreen mode

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().

console group adn groupEnd exampls

9. console.warn()

Logging warnings in the console should be easy! Thats why the console.warn command exists



console.warn("Some warning");


Enter fullscreen mode Exit fullscreen mode

console warn example

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


Enter fullscreen mode Exit fullscreen mode

console.times example

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

Buy Me A Coffee

💖 💪 🙅 🚩
josiasaurel
Josias Aurel

Posted on December 3, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

JavaScript qanday dasturlash tili ?
javascript JavaScript qanday dasturlash tili ?

December 14, 2023

#LearnedToday: EyeDropper Browser API
javascript #LearnedToday: EyeDropper Browser API

August 21, 2023

11 JavaScript Console Commands Explained
javascript 11 JavaScript Console Commands Explained

December 3, 2020

User-Agent (browser) detection
javascript User-Agent (browser) detection

August 13, 2019