Output with console.log
Use console.log() to display values, text and results.
Syntax
console.log(value1, value2, ...);The main way to see results in these lessons is console.log(). You can pass it text, numbers, or any value, and you can pass several values separated by commas.
Logging different things
- Text (a string) goes in quotes.
- Numbers are written without quotes.
- Multiple arguments are printed on one line, separated by spaces.
Example
Loading editor…
Press Run to execute the code.
When to use it
- Log the response body of a failed network request to diagnose API errors during development.
- Print intermediate calculation results to the console while debugging a pricing formula.
- Write structured objects to the console to inspect state changes in a shopping cart.
More examples
Log a simple string
Outputs a plain string message to the browser or Node.js console.
console.log("Server started successfully");Log multiple values at once
Passes multiple comma-separated values to console.log so they appear on one line with labels.
const price = 29.99;
const tax = price * 0.1;
console.log("Price:", price, "Tax:", tax, "Total:", price + tax);Inspect a nested object
Logs a nested object with console.log for a quick look and console.dir for full-depth inspection.
const user = { id: 1, name: "Alice", address: { city: "Oslo" } };
console.log(user);
console.dir(user, { depth: null });
Discussion