JavaScript console.log() Method
In JavaScript, console.log() method is a built-in JavaScript function that outputs messages to the console, which is a special area in web browsers or runtime environments for developers to view information about their code.
It is primarily used for:
- Debugging code
- Inspecting variables
- Tracking the flow of execution
- Logging errors or warnings
Syntax:
console.log("");
- message: The value, variable, or expression you want to print.
- Message can be a string, number, object, array, or any valid JavaScript expression.
- It returns the value of the parameter given.
Using Console.log()
The console.log() method is one of the most widely used debugging tools in JavaScript. It prints messages, variables, and expressions to the console, helping developers understand how their code is running.
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
The console.log() method can log not only strings and numbers but also complex data types like objects and arrays. This makes it especially useful when debugging applications.
- Logging an Object: When you log an object directly, the console shows it in an expandable format, so you can explore its properties:
let user = {
name: "Alice",
age: 25,
city: "New York"
};
console.log(user);
Output
{ name: "Alice", age: 25, city: "New York" }
- Logging an Array: Arrays can also be logged directly. The console shows them as a list of values with their index positions:
let fruits = ["Apple", "Banana", "Cherry"];
console.log(fruits);
Output
["Apple", "Banana", "Cherry"]