console.log basics
Learn to log output and pass multiple arguments with console.log.
console.log basics
In JavaScript console.log is the most used output method. It writes the value inside the parentheses to the console. You can pass a single value or several values; a space is automatically added between them.
Example: console.log("Hello", "World"). This call writes Hello World to the console. Numbers, arrays and objects can be printed the same way.
Multiple arguments and variables
To print several values side by side you pass comma-separated arguments to console.log. A space is automatically placed between them.
You can pass variables the same way: console.log("Total:", total). Here the value of total appears in the output. This is more readable than joining values with +.
Exercise Print product info
Assign "Pen" to a variable called product. Assign 25 to a second variable called price. Then call console.log with the arguments "Product:", product, "Price:" and price.
Show example solution
const product = "Pen";
const price = 25;
console.log("Product:", product, "Price:", price);
Test yourself
-
Which is the output of console.log("Hello", "World")?
console.log("Hello", "World")- HelloWorld
- Hello World
- Hello, World
- Hello World
Show answer
Hello World — Correct. console.log puts a space between arguments.
-
Add the right argument
const total = 50; console.log("Toplam: ", ____);Show answer
const total = 50; console.log("Toplam: ", total); -
Match the call to the output
- console.log("Hi")
- Hi
- console.log("A", "B")
- A B
- console.log(1 + 2)
- 3
- console.log("Result: " + 5)
- Result: 5