In JavaScript, console is a built-in object that provides access to the browser's debugging console. It is commonly used to output information, errors, or messages to help developers understand code behavior during development.
Common Console Methods
| Method | Description |
|---|---|
console.log() | Outputs general messages or values. |
console.error() | Outputs error messages in red. |
console.warn() | Outputs warning messages in yellow. |
console.info() | Outputs informational messages. |
console.clear() | Clears the console screen. |
Using console.log()
This is the most common way to display output. It can take multiple arguments and print them on the console separated by spaces.
📌 Deep Dive: Basic console.log()
console.log('Hello, world!');
console.log('Number:', 42);
console.log('Sum:', 5 + 3);
Number: 42
Sum: 8
Formatted Output with Placeholders
You can format messages using substitution strings like %s for strings, %d or %i for integers, and %f for floating-point numbers.
📌 Deep Dive: Using Placeholders
const name = 'Alice';
const age = 30;
console.log('Name: %s, Age: %d', name, age);

💡 Why Use Console Output?
Console output helps you check variable values, trace code execution, and debug problems without interrupting program flow.
Other Useful Console Methods
console.error(): Use it to highlight errors, often shown in red in the console.console.warn(): Displays warnings, useful for cautionary messages.console.info(): For informational messages, sometimes styled differently.console.clear(): Clears the entire console screen.
📌 Deep Dive: Error and Warning Example
console.error('This is an error!');
console.warn('This is a warning!');
console.info('Some info for you.');
This is a warning!
Some info for you.
⚠️ Note on Console Output in Production
Excessive console output can clutter your browser's console and impact performance. It's best to remove or limit console statements before deploying your code.
Summary
console.log()is your primary method to output messages for debugging.- Use
console.error()andconsole.warn()to highlight issues. - Placeholders help format output cleanly.
- Remember to clear or disable console output in production environments.
Quick Knowledge Check
Test what you just learned
Question 1 of 2
Which console method is best for displaying general debug information?
Question 2 of 2
What placeholder would you use to insert an integer into a formatted console message?
Loading results...