Type checking using typeof in Javascript - Time & Space Complexity
We want to understand how long it takes to check the type of a value using typeof in JavaScript.
Specifically, how does the time grow when we check many values?
Analyze the time complexity of the following code snippet.
const values = [1, 'hello', true, null, undefined, {}, []];
for (let i = 0; i < values.length; i++) {
console.log(typeof values[i]);
}
This code loops through an array and prints the type of each element using typeof.
- Primary operation: The
forloop that runstypeofon each element. - How many times: Once for each element in the array.
As the number of elements grows, the number of typeof checks grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 typeof checks |
| 100 | 100 typeof checks |
| 1000 | 1000 typeof checks |
Pattern observation: The work grows directly with the number of items; double the items, double the checks.
Time Complexity: O(n)
This means the time to check types grows in a straight line with the number of elements.
[X] Wrong: "Using typeof is instant and does not depend on the number of elements."
[OK] Correct: While one typeof is very fast, checking many elements means repeating it many times, so total time grows with the number of elements.
Understanding how simple operations like typeof scale helps you reason about performance in real code, showing you can think about efficiency clearly.
"What if we replaced the for loop with a recursive function calling typeof on each element? How would the time complexity change?"
Practice
typeof operator return when used on the value 42?Solution
Step 1: Understand the value type
The value42is a number in JavaScript.Step 2: Apply
Usingtypeofoperatortypeof 42returns the string "number" because 42 is a numeric value.Final Answer:
"number" -> Option DQuick Check:
typeof 42= "number" [OK]
- Thinking numbers return "string"
- Confusing number with object
- Using typeof without parentheses
x is a string using typeof?Solution
Step 1: Check correct operator usage
The strict equality operator is===, sotypeof x === 'string'is correct.Step 2: Check string quotes and parentheses
The type name must be a string literal in quotes, and parentheses aroundxare optional.Final Answer:
if (typeof x === 'string') {} -> Option BQuick Check:
Use=== 'string'with quotes [OK]
- Using single equals (=) instead of triple (===)
- Missing quotes around 'string'
- Using typeof without quotes for type
console.log(typeof null);
Solution
Step 1: Understand the value
In JavaScript,nullnullis a special value representing "no object".Step 2: Apply
Due to a historical bug,typeoftonulltypeof nullreturns "object" instead of "null".Final Answer:
"object" -> Option CQuick Check:
typeof null= "object" [OK]
- Expecting "null" as output
- Confusing with "undefined"
- Thinking null is a boolean
let val = 10;
if (typeof val = "number") {
console.log("It's a number");
}
Solution
Step 1: Check the if condition syntax
The condition uses=which is assignment, not comparison.Step 2: Correct operator for comparison
Use===or==to compare the result oftypeof valwith "number".Final Answer:
Using single equals (=) instead of comparison operator -> Option AQuick Check:
Use===for comparison, not=[OK]
- Using = instead of === in if conditions
- Thinking typeof returns variable type directly
- Ignoring quotes around type string
isStringOrNumber that returns true if the input is either a string or a number, and false otherwise. Which of the following implementations is correct?Solution
Step 1: Understand the requirement
The function should return true if the value is a string OR a number.Step 2: Check logical operators and comparisons
function isStringOrNumber(value) { return typeof value === 'string' || typeof value === 'number'; } uses||(OR) and strict equality===, which is correct.Step 3: Identify errors in other options
function isStringOrNumber(value) { return typeof value === 'string' && typeof value === 'number'; } and the option using==uses AND&&, which requires both types simultaneously (impossible). function isStringOrNumber(value) { return typeof value = 'string' || typeof value = 'number'; } uses assignment=instead of comparison.Final Answer:
function isStringOrNumber(value) { return typeof value === 'string' || typeof value === 'number'; } -> Option AQuick Check:
Use OR (||) and === for type checks [OK]
- Using && instead of || for OR condition
- Using = instead of === for comparison
- Mixing == and === inconsistently
Ready to prove it?
Pass the test and this pattern moves unknown
