Output using console.log in Javascript - Time & Space Complexity
Let's see how the time it takes to show messages with console.log changes as we print more messages.
We want to know how the work grows when we output many lines.
Analyze the time complexity of the following code snippet.
for (let i = 0; i < n; i++) {
console.log(i);
}
This code prints numbers from 0 up to n-1 using console.log.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The loop runs and calls console.log each time.
- How many times: Exactly n times, once for each number.
As n grows, the number of console.log calls grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 console.log calls |
| 100 | 100 console.log calls |
| 1000 | 1000 console.log calls |
Pattern observation: The work grows directly with the number of messages printed.
Time Complexity: O(n)
This means if you print twice as many messages, it takes about twice as long.
[X] Wrong: "console.log runs instantly and does not affect time."
[OK] Correct: Each console.log takes some time, so printing more lines adds up and slows the program.
Understanding how output operations grow helps you think about program speed and efficiency in real tasks.
"What if we printed only every other number instead of all? How would the time complexity change?"
Practice
console.log do in JavaScript?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]
- Thinking console.log stops the program
- Confusing console.log with input functions
- Assuming console.log creates variables
console.log?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]
- Using console.print instead of console.log
- Missing parentheses
- Using colon instead of parentheses
let x = 5;
let y = 10;
console.log(x + y);
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]
- Thinking + concatenates as strings here
- Printing variable names instead of values
- Expecting undefined output
console.log('Hello World'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]
- Ignoring missing parenthesis
- Thinking semicolon is mandatory error
- Confusing quotes with parentheses
let a = 3;
let b = '4';
console.log(a + b);
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]
- Expecting numeric sum
- Thinking it causes error
- Ignoring type conversion rules
Ready to prove it?
Pass the test and this pattern moves unknown
