JavaScript console.log()

JavaScript console.log()

All modern browsers have a web console for debugging. The console.log() method is used to write messages to these consoles. For example,

let sum = 44;
console.log(sum);   // 44

When you run the above code, 44 is printed on the console.

To learn more about using a console, visit: JavaScript Getting Started.


console.log() Syntax

Its syntax is:

console.log(message);

Here, the message refers to either a variable or a value.


Note: We will be using the console.log() method to display the output in our upcoming lessons.


Example 1: Print a Sentence

// program to print a sentence

// passing string
console.log("I love JS");

Output

I love JS

Example 2: Print Values Stored in Variables

// program to print variables values

// storing values
const greet = 'Hello';
const name = 'Jack';

console.log(greet + ' ' + name);

Output

Hello Jack

As you can see from these examples, console.log() makes it easier to see the value inside a variable. That's why it's commonly used for testing/debugging code.

The console object also has various methods other than console.log(). To learn more, visit JavaScript console.

Did you find this article helpful?