🏠 JavaScript Basics
Estimated reading: 3 minutes 349 views

JavaScript console.log() – How to Use It for Debugging and Output


Introduction: Why Use console.log()?

Ever wondered how developers check values, trace errors, or understand code flow in JavaScript?
That’s where console.log() shines!

It’s the most commonly used debugging method in JavaScript, allowing you to output data to the browser console—an essential tool for developers at every level.

By the end of this guide, you’ll know:

What console.log() does
How to use it with strings, numbers, variables, and objects
Tips for debugging and formatting
Common mistakes to avoid


What Is console.log()?

The console.log() method is part of the Console API, provided by the browser for developers to log messages and data while running scripts.

codeconsole.log("Hello, JavaScript!");

Output:

Hello, JavaScript!

Note: This output appears in your browser’s developer console, not on the webpage.


Syntax of console.log()

codeconsole.log(value1, value2, ..., valueN);
  • value1...valueN: One or more values (strings, numbers, variables, objects, arrays, etc.)

Examples and Use Cases

1️⃣ Logging Strings

codeconsole.log("This is a string message");

✔️ Logs plain text messages.


2️⃣ Logging Numbers

codeconsole.log(42);
console.log(3.14);

✔️ Useful for checking calculations and numeric outputs.


3️⃣ Logging Variables

codelet userName = "Alex";
console.log(userName);

✔️ Helps track variable values in runtime.


4️⃣ Logging Multiple Values

codelet age = 25;
let city = "Mumbai";
console.log("Age:", age, "| City:", city);

✔️ Combines labels and values for better clarity.


5️⃣ Logging Objects & Arrays

codelet person = { name: "John", age: 30 };
let colors = ["red", "green", "blue"];

console.log(person);
console.log(colors);

✔️ Great for viewing structured data.


6️⃣ Logging Inside Functions

codefunction greet(name) {
console.log("Hello " + name);
}
greet("Sam");

✔️ Trace function outputs and arguments.


Best Practices

Label your logs:

codeconsole.log("User Age:", age);

Group logs with console.group() and console.groupEnd()

Use console.table() for tabular data

Clear console with console.clear() if needed


Common Mistakes

Writing Console.log() (wrong capitalization)
Using it in production code
Expecting it to print on the webpage


Related Console Methods

MethodUse Case
console.error()Displays an error message (in red)
console.warn()Displays a warning message (in yellow)
console.table()Logs tabular data
console.dir()Logs object in a DOM-like structure
console.assert()Logs a message only if assertion fails

Summary

  • console.log() helps debug, monitor values, and understand code behavior.
  • It supports multiple data types.
  • It’s a go-to tool for developers while coding in the browser environment.

FAQ – Console.log() in JavaScript

Q1: What does console.log() do?

A: It prints messages to the browser console for debugging and informational purposes.

Q2: Can I use console.log() in production?

A: It’s not recommended—remove or disable logs before deployment to keep the console clean and secure.

Q3: Can I log multiple values at once?

A: Yes! Separate them with commas:

codeconsole.log("User:", user, "Score:", score);

Q4: Where do I see the output?

A: Open your browser’s DevTools (F12) and check the Console tab.

Q5: What’s the difference between console.log() and alert()?

A:

  • console.log() logs in DevTools (non-intrusive)
  • alert() shows a blocking popup (interrupts user flow)

Share Now :
Share

JavaScript — Console.log()

Or Copy Link

CONTENTS
Scroll to Top