JavaScript console.log() Method
The console.log() method in JavaScript logs messages or data to the console.
The console.log() method is useful for debugging or testing purposes.
Syntax:
console.log("");Parameters:
Any message either number, string, array object, etc.
Return value:
It returns the value of the parameter given.
Using Console.log()
Using the console.log() to print a string on the console.
console.log("Hello Geeks")
Output
Hello Geeks
We can even print an array or an object on the console using log() method
Logging a Object and Array
// Logging an Object
const myObject = {Skill1:"HTML", Skill2:"CSS"};
console.log(myObject);
// Logging an array
const myArray = ["HTML", "CSS", "JavaScript"];
console.log(myArray);
Output
{ Skill1: 'HTML', Skill2: 'CSS' }
[ 'HTML', 'CSS', 'JavaScript' ]
Supported Browsers:
JavaScript console.log() Method – FAQs
How do I print multiple values with console.log()?
You can print multiple values by separating them with commas:
console.log("Value 1:", value1, "Value 2:", value2);
Can I format output with console.log()?
Yes, you can use format specifiers like %s, %d, and %c in console.log():
console.log("%cStyled text", "color: blue; font-size: 20px;");
How do I use console.log() to debug JavaScript code?
You can insert console.log() statements at different points in your code to check variable values, inspect objects, or understand the flow of execution:
console.log("Variable value:", myVariable);
Is console.log() asynchronous or synchronous?
console.log() is synchronous, meaning it executes immediately in the current thread before the next line of code runs.
How do I log errors with console.log()?
Although you can log errors with console.log(), it’s better to use console.error() for error messages, as it differentiates them visually (often in red) and is easier to filter in the console.


