JavaScript : Console commands

benjaminklein99

Benjamin Klein

Posted on March 9, 2023

JavaScript : Console commands

The Console

The console is a vital tool that every JavaScript developer should keep in their tool-belt for their day to day work. It allows us to peer into the state of our code and helps us debug. Here are few handy console commands you should use in your code.

console.log()

The first console command I should introduce you to is the console log command. This versatile command allows us to 'log' messages to the console, as well as examine the value that is stored in a variable. Here's a few examples of how to use console.log

var message = 'Hello from the console!'
console.log(message); // logs ==> Hello from the console!

var value = 9;
console.log(value); // logs ==> 9
Enter fullscreen mode Exit fullscreen mode

We can also log multiple values at the same time by passing their variable names into the parenthesis separated with commas.

var string1 = 'Hello';
var string2 = 'World';
console.log(string1, string2); // logs ==> Hello World
Enter fullscreen mode Exit fullscreen mode

console.table()

Another very useful console command is "console.table()". This command is great when working with an array of objects. It logs a table to the console that describes what key and value each object has and where it is in the array. This tool is not only helpful when navigating an array of objects, but also when building them, as it allows the user to easily discern if you have reached your desired outputs. Below is an example of how we can invoke the console.table() command and an explanation of how to interpret the tables contents.

let arrayOfObjects = [{a: 10}, {b: 20}, {c: 30}]
console.table(arrayOfObjects);
// logs ==>
┌─────────┬────┬────┬────┐
│ (index) │ a  │ b  │ c  │
├─────────┼────┼────┼────┤
│    0    │ 10 │    │    │
│    1    │    │ 20 │    │
│    2    │    │    │ 30 │
└─────────┴────┴────┴────┘


var arrayOfObjects2 = [{a: 10, b: 20}, {c: 30, d: 40}, {e: 50, f:60}]
console.table(arrayOfObjects2);
// logs ==> 
┌─────────┬────┬────┬────┬────┬────┬────┐
│ (index) │ a  │ b  │ c  │ d  │ e  │ f  │
├─────────┼────┼────┼────┼────┼────┼────┤
│    0    │ 10 │ 20 │    │    │    │    │
│    1    │    │    │ 30 │ 40 │    │    │
│    2    │    │    │    │    │ 50 │ 60 │
└─────────┴────┴────┴────┴────┴────┴────┘


var arrayOfObjects3 = [{a: 10}, {a: 20}, {a: 30}]
console.table(arrayOfObjects3);
//logs ==>
┌─────────┬────┐
│ (index) │ a  │
├─────────┼────┤
│    0    │ 10 │
│    1    │ 20 │
│    2    │ 30 │
└─────────┴────┘
Enter fullscreen mode Exit fullscreen mode

The row across the top of the table represents the keys that are present in each object. Bellow each key, in the keys corresponding column, is the value at that key. To find the index at which the object exists in the array, find the key that exists in the desired object, follow the keys column down to its value, and then follow the values row to the left to find the index.

console.clear()

The final command I will discuss in this post is the console clear command. As we create more and more messages and tables, the console can become cluttered and tedious to sift through. To invoke this command, we simply write console.clear(). This command takes no inputs so nothing needs to be written into the parenthesis. When this command is invoked, all contents of the console are removed.

Now You Know!

Now you know a thing or two about the console and how it can make both coding and debugging a bit easier. Go try implementing some of these commands in your code and see what they can do for you. Happy coding!

💖 💪 🙅 🚩
benjaminklein99
Benjamin Klein

Posted on March 9, 2023

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

Sign up to receive the latest update from our blog.

Related

What was your win this week?
weeklyretro What was your win this week?

November 29, 2024

Where GitOps Meets ClickOps
devops Where GitOps Meets ClickOps

November 29, 2024

How to Use KitOps with MLflow
beginners How to Use KitOps with MLflow

November 29, 2024

Modern C++ for LeetCode 🧑‍💻🚀
leetcode Modern C++ for LeetCode 🧑‍💻🚀

November 29, 2024