JavaScript console.log() Method

The console.log() method is a built-in JavaScript function that “writes a message to the console”. It accepts multiple arguments, which will be converted to strings and concatenated with a space character between them.

To enable more advanced formatting, it can also accept format specifiers, such as %s for strings, %d or %i for integers, and %f for floating-point numbers.

The console.log() method helps debug and development purposes, allowing developers to output messages, variables, and other information to the console for inspection.

Syntax

console.log(message)

Parameters

“message”: The parameter is required, which is the message to write to the console.

Example

console.log("Homer, Simpson!");

const fname = "Bart Simpson";
const age = 9;

console.log('My name is', fname, 'and I am', age, 'years old.');

console.log('My name is %s and I am %d years old.', fname, age);

Output

Homer, Simpson!
My name is Bart Simpson and I am 9 years old.
My name is Bart Simpson and I am 9 years old.

In this code example, we used the console.log() function to output a simple string, then output a message with variables, and finally output a message with format specifiers for more advanced formatting.

Remember that console.log() method is intended for development and debugging purposes only, and it’s not recommended to use it in production code.

Some web browsers may not support it, or users may have their console closed or disabled. It’s generally best to remove or comment out console.log() statements before deploying your code to production.

Leave a Comment