Console.log and Basic Output
You'll learn how to display values, strings, and objects in your browser's developer console—the most essential debugging tool in JavaScript.
What Is Console.log?
console.log() is a method that outputs information to the browser's console (or Node.js terminal). Think of it as a window into your code's execution—you can peek at variable values, check if functions run, and track what's happening behind the scenes.
The console is your debugging companion. Unlike alert() which interrupts users with a popup, console.log() quietly records messages in the developer tools where only you (the developer) can see them.
Basic Usage
// Logging a simple string
console.log("Hello from the console!");
// Logging numbers
console.log(42);
// Logging variables
const userName = "Alice";
console.log(userName);
// Multiple values at once
console.log("User:", userName, "Age:", 25);
Logging Objects and Arrays
console.log() shines when inspecting complex data structures:
const user = { name: "Bob", age: 30, active: true };
console.log(user); // Shows expandable object
const numbers = [1, 2, 3, 4, 5];
console.log(numbers); // Shows array contents
The browser formats objects and arrays interactively, letting you expand nested properties.
Why It Matters
You'll use console.log() constantly to:
- Debug unexpected behavior
- Verify values at specific points
- Understand code flow
- Inspect API responses or DOM elements
Key Takeaway: console.log() is your primary tool for outputting values, strings, and objects to the browser console—master it early, as you'll use it in nearly every development session.