The array length property tells you how many items are in the array. It helps you know the size of the list you are working with.
Array length property in Javascript
const array = ['item1', 'item2', 'item3']; const size = array.length;
The length property is always one more than the highest index because arrays start at 0.
You can read length to get the number of items, or set it to change the array size.
const fruits = ['apple', 'banana', 'cherry']; console.log(fruits.length);
const emptyArray = []; console.log(emptyArray.length);
const oneItem = ['only']; console.log(oneItem.length);
const numbers = [10, 20, 30, 40]; numbers.length = 2; console.log(numbers);
This program shows how to get the length of an array, use it to loop through all items, and then change the length to remove items.
const colors = ['red', 'green', 'blue', 'yellow']; console.log('Original array:', colors); console.log('Length:', colors.length); // Loop through the array using length for (let index = 0; index < colors.length; index++) { console.log(`Color at index ${index}:`, colors[index]); } // Change length to remove last two colors colors.length = 2; console.log('Array after changing length:', colors); console.log('New length:', colors.length);
Accessing length is very fast, O(1) time complexity.
Changing length to a smaller number removes items from the end, which frees memory.
Common mistake: expecting length to count only non-empty or non-undefined items, but it counts all indexes.
Use length when you need to know or control the size of the array quickly.
The length property tells how many items are in an array.
You can read it to count items or set it to change the array size.
It is useful for loops, checks, and managing array size.
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
