We use console.log to show messages or information on the screen while running JavaScript. It helps us see what the program is doing.
Output using console.log in Javascript
Introduction
Syntax
Javascript
console.log(value1, value2, ..., valueN);
You can print one or many values separated by commas.
The values can be text, numbers, variables, or expressions.
Examples
Javascript
console.log('Hello, world!');
Javascript
console.log(5 + 3);
Javascript
let name = 'Alice'; console.log('Name:', name);
Javascript
console.log('Sum:', 2 + 2, 'and product:', 2 * 3);
Sample Program
This program prints a welcome message, then shows the current age and the age next year.
Javascript
console.log('Welcome to JavaScript!'); let age = 25; console.log('Age is', age); console.log('Next year, age will be', age + 1);
Important Notes
console.log prints to the browser console or terminal, not on the webpage itself.
You can open the browser console by pressing F12 or right-click and choose 'Inspect' then go to the 'Console' tab.
Use console.log often to understand what your code is doing step-by-step.
Summary
console.log shows messages or values in the console.
It helps you see what your program is doing while it runs.
You can print text, numbers, variables, or many things at once.
Practice
1. What does
console.log do in JavaScript?easy
Solution
Step 1: Understand the purpose of console.log
console.logis used to show messages or values in the console, helping to see what the program does.Step 2: Compare options with the purpose
Only It prints messages or values to the console. matches this purpose. Other options describe different actions.Final Answer:
It prints messages or values to the console. -> Option CQuick Check:
console.log prints output [OK]
Hint: Remember: console.log shows info in the console [OK]
Common Mistakes:
- Thinking console.log stops the program
- Confusing console.log with input functions
- Assuming console.log creates variables
2. Which of the following is the correct syntax to print "Hello World" using
console.log?easy
Solution
Step 1: Identify correct console.log syntax
The correct way to print isconsole.log('text')with parentheses and quotes around the text.Step 2: Check each option
console.log('Hello World') uses correct syntax. Options A, B, and C use wrong method names or punctuation.Final Answer:
console.log('Hello World') -> Option DQuick Check:
console.log('text') is correct syntax [OK]
Hint: Use console.log() with parentheses and quotes [OK]
Common Mistakes:
- Using console.print instead of console.log
- Missing parentheses
- Using colon instead of parentheses
3. What will be the output of the following code?
let x = 5;
let y = 10;
console.log(x + y);
medium
Solution
Step 1: Understand the variables and operation
Variables x and y hold numbers 5 and 10. The expressionx + yadds these numbers.Step 2: Calculate the sum
5 + 10 equals 15, soconsole.log(x + y)prints 15.Final Answer:
15 -> Option AQuick Check:
5 + 10 = 15 [OK]
Hint: Add numbers inside console.log to get sum [OK]
Common Mistakes:
- Thinking + concatenates as strings here
- Printing variable names instead of values
- Expecting undefined output
4. Find the error in this code:
console.log('Hello World'medium
Solution
Step 1: Check the syntax of console.log
The function call must have matching opening and closing parentheses.Step 2: Identify the missing part
The code misses the closing parenthesis after the string, causing a syntax error.Final Answer:
Missing closing parenthesis -> Option AQuick Check:
Parentheses must match in function calls [OK]
Hint: Count opening and closing parentheses carefully [OK]
Common Mistakes:
- Ignoring missing parenthesis
- Thinking semicolon is mandatory error
- Confusing quotes with parentheses
5. What will this code print?
let a = 3;
let b = '4';
console.log(a + b);
hard
Solution
Step 1: Understand the types of variables
Variable a is a number 3, b is a string '4'.Step 2: Apply + operator with mixed types
When adding number + string, JavaScript converts number to string and concatenates, so 3 + '4' becomes '34'.Final Answer:
34 -> Option BQuick Check:
Number + string concatenates as string [OK]
Hint: Number + string joins as text, not sums [OK]
Common Mistakes:
- Expecting numeric sum
- Thinking it causes error
- Ignoring type conversion rules
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
