JavaScript console is a dialog box or area where we can print our messages & data and run JS code in general.
The work that happens while our code executes is not visible to us. So, developers use the console to print out messages making sure they’re on the right track and figure out bugs & errors in their code. This process is called Debugging.
From the previous lesson, we learned that we can open the console on our browser by pressing the F12 key.
Another way is by right clicking > selecting Inspect option> clicking on Console Tab in the DevTools.
We can view the source code of any website in the world by opening the website on our desired browser and pressing the F12 key 🤓.
JavaScript console.log() 🤯
We can print our messages and data using the console.log() method. The console keyword is an object. Note: An object is a collection of data and actions. We’ll learn that later on our Classes & Objects lesson and the log() keyword is a method [Note: A method is an action that does some stuff.
Keywords are words that are understood by the specific programming language they’re built into and do a specific task. Here, the log() method logs or prints out any messages or data we want.
Syntax
console.log(message);
//Inside (), we put the message or data we want to display
Inside the parenthesis i.e. () we put the message or data that we want to display.
Example 1: Print the text “Hello World” using console.log() 👋
console.log("Hello, World");
Output
Hello, World
Example 2: Print numbers using console.log() 🔢
console.log(100);
console.log(9);
console.log(000);
Output
100
9
0
JavaScript automatically understands that “000” means 0. So, it prints out only 0.
In this way, console.log() lets us print out messages which makes it really fun and interactive to use. It has more functions than just displaying messages. To find out more, you can view the official Mozilla Docs on console.log().