Array length property in Javascript - Time & Space Complexity
We want to understand how fast we can get the size of an array using its length property.
How does the time to find the length change as the array grows?
Analyze the time complexity of the following code snippet.
const arr = [1, 2, 3, 4, 5];
const size = arr.length;
console.log(size);
This code gets the length of an array and prints it.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing the
lengthproperty of the array. - How many times: Exactly once, no loops or repeated steps.
Getting the length does not depend on how big the array is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The operation count stays the same no matter the array size.
Time Complexity: O(1)
This means getting the array length takes the same small amount of time no matter how big the array is.
[X] Wrong: "Getting the length takes longer if the array is bigger because it counts all items each time."
[OK] Correct: The length is stored as a property, so it is instantly available without counting.
Knowing that array length access is very fast helps you write efficient code and answer questions confidently.
"What if we loop through the array to count items instead of using length? How would the time complexity change?"
Practice
What does the length property of a JavaScript array represent?
Solution
Step 1: Understand the
Thelengthpropertylengthproperty counts how many items are stored in the array.Step 2: Differentiate from other properties
It does not give the highest index or size in bytes, only the count of elements.Final Answer:
The number of elements in the array -> Option DQuick Check:
Array length = number of elements [OK]
- Confusing length with highest index
- Thinking length gives memory size
- Assuming length returns last element
Which of the following is the correct way to get the length of an array arr in JavaScript?
Solution
Step 1: Recall the syntax for accessing length
Thelengthproperty is accessed without parentheses because it is a property, not a function.Step 2: Check each option
arr.length usesarr.lengthcorrectly. arr.length() uses parentheses incorrectly. Options C and D are not valid JavaScript syntax for array length.Final Answer:
arr.length -> Option BQuick Check:
Use property, not function: arr.length [OK]
- Using arr.length() with parentheses
- Trying length(arr) like a function
- Using arr.size which doesn't exist
What will be the output of the following code?
const fruits = ['apple', 'banana', 'cherry']; console.log(fruits.length);
Solution
Step 1: Count the elements in the array
The arrayfruitshas three elements: 'apple', 'banana', and 'cherry'.Step 2: Use the length property
Accessingfruits.lengthreturns the number of elements, which is 3.Final Answer:
3 -> Option CQuick Check:
Array length = 3 [OK]
- Counting indexes starting from 0
- Expecting length to be last index
- Confusing length with undefined
Identify the error in the following code snippet:
let numbers = [1, 2, 3, 4]; numbers.length = 2; console.log(numbers.length); console.log(numbers[2]);
Solution
Step 1: Understand setting length property
Settingnumbers.length = 2truncates the array to first two elements, removing elements after index 1.Step 2: Check the output of
Since the array is truncated,numbers[2]numbers[2]is nowundefined, not 3.Final Answer:
Setting length to 2 removes elements after index 1 -> Option AQuick Check:
Changing length truncates array [OK]
- Thinking length is read-only
- Expecting old elements to remain after truncation
- Assuming numbers[2] still holds value
Given the array let data = [10, 20, 30, 40, 50];, which code snippet correctly doubles the array size by adding undefined elements at the end?
// Choose the correct code A) data.length = data.length * 2; B) data.length = data.length + data.length; C) data.length = data.length * 2; data.fill(undefined, 5, data.length); D) data.length = data.length * 2; data.fill(undefined);
Solution
Step 1: Double the length property
Settingdata.length = data.length * 2doubles the array size but new slots are empty (holes).Step 2: Fill new slots with
Usingundefineddata.fill(undefined, 5, data.length)fills the new slots from index 5 to the new length withundefined.Step 3: Check other options
A and B only change length but do not fill new slots, leaving holes. D usesdata.fill(undefined)which fills from index 0, overwriting original data.Final Answer:
data.length = data.length * 2; data.fill(undefined, 5, data.length); -> Option AQuick Check:
Double length then fill new slots [OK]
- Not filling new slots, leaving holes
- Overwriting existing elements with fill
- Using fill without end index
Ready to prove it?
Pass the test and this pattern moves unknown
