Writing first JavaScript program - Time & Space Complexity
When we write our first JavaScript program, it is important to see how the time it takes to run changes as we add more code or data.
We want to know how the program's work grows when we change its size or input.
Analyze the time complexity of the following code snippet.
console.log('Hello, world!');
This code simply prints a message once to the screen.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: One single print to the console.
- How many times: Exactly once, no loops or repeats.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 1 | 1 print |
| 10 | 1 print |
| 100 | 1 print |
Pattern observation: No matter how big the input is, the program only prints once, so the work stays the same.
Time Complexity: O(1)
This means the program takes the same amount of time no matter how big the input is.
[X] Wrong: "Adding more data to the program will make it slower even if the code does not repeat."
[OK] Correct: If the program only does one action, like printing once, adding data that is not used does not change the time it takes.
Understanding how simple programs run helps build a strong base for more complex coding tasks later on.
"What if we added a loop to print the message multiple times? How would the time complexity change?"
Practice
console.log() function do in JavaScript?Solution
Step 1: Understand the purpose of
This function is used to display messages or values in the browser's console, which helps developers see output or debug.console.log()Step 2: Compare with other options
Options A, B, and D describe different actions thatconsole.log()does not perform.Final Answer:
It shows messages in the browser console. -> Option DQuick Check:
console.log()shows messages [OK]
- Thinking console.log creates alerts
- Confusing console.log with styling commands
- Assuming console.log stops the program
Solution
Step 1: Identify JavaScript syntax for output
JavaScript usesconsole.log()to print messages to the console.Step 2: Check other options for language mismatch
A is Python style, B is Java style, C is PHP style, so they are incorrect for JavaScript.Final Answer:
console.log("Hello, World!"); -> Option BQuick Check:
JavaScript output uses console.log() [OK]
- Using print() instead of console.log()
- Confusing JavaScript with other languages
- Missing parentheses or semicolon
console.log('Hi');
console.log('There');Solution
Step 1: Understand console.log behavior
Eachconsole.log()prints its message on a new line in the console.Step 2: Analyze the output lines
The first prints "Hi", then the second prints "There" on the next line, so output is two lines: "Hi" then "There".Final Answer:
Hi There -> Option AQuick Check:
console.log prints each message on a new line [OK]
- Thinking console.log joins messages on one line
- Confusing \n as literal text instead of newline
- Ignoring line breaks between calls
console.log('Hello World')Solution
Step 1: Check syntax correctness
The code uses correct function name, quotes, and parentheses.Step 2: Consider JavaScript semicolon rules
Semicolons are optional at the end of statements in JavaScript, so missing semicolon is not an error.Final Answer:
No error, code is correct -> Option CQuick Check:
JavaScript allows optional semicolons [OK]
- Thinking missing semicolon causes error
- Confusing quote types as error
- Assuming console.log is misspelled
Solution
Step 1: Understand printing on separate lines
Using twoconsole.log()calls prints each message on its own line.Step 2: Check other options for line breaks
console.log('Name: John\nAge: 25'); uses \n which creates a newline in the output; console.log('Name: John' + 'Age: 25'); joins to 'Name: JohnAge: 25' on one line; console.log('Name: John Age: 25'); prints all on one line.Final Answer:
console.log('Name: John'); console.log('Age: 25'); -> Option AQuick Check:
Separate console.log calls print on separate lines [OK]
- Using + without spaces causing joined text
- Thinking \n creates a newline (prints literal \n)
- Printing all info in one console.log without breaks
Ready to prove it?
Pass the test and this pattern moves unknown
