JavaScript Array length
The array length in JavaScript is a 32-bit integer indicating the number of elements in an array, always one more than the highest index due to zero-based indexing. The array length in JavaScript can reach up to 2^32 (4,294,967,296 elements), providing a vast range of array sizes. This property is essential for iterating over arrays and dynamically managing their contents, making it a fundamental aspect of array manipulation in JavaScript.
Syntax of array length in JavaScript
This syntax is used to return the length of the array:
Similarly, we can use this property to set the length of the array:
value
Attribute | Description |
---|---|
Value | A nonnegative integer less than 2^32. |
Writable | Yes |
Enumerable | No |
Configurable | No |
- Writable: The property's value cannot be altered if this attribute is set to false.
- Enumerable: If this attribute is set to true, for or for...in loops will iterate through the property.
- Configurable: Suppose if the attribute is assigned as false.
Description
The length property of a JavaScript array reflects the number of elements it contains, represented as a nonnegative integer not exceeding 2^32 - 1. This dynamic property is pivotal in managing array sizes and content.
Consider the examples:
In these examples, fruits.length returns 3, indicating three elements in the array. Similarly, numbers.length returns 5, as it was initialized with five slots.
Key Behaviors
- Truncating Arrays: Reducing the length property's value will truncate the array, removing elements beyond the new length.
- Extending Arrays: Setting the length to a larger number expands the array with empty slots, not undefined values. These slots are peculiar because they don't hold any value, even undefined.
- Invalid Lengths: Assigning a negative number, a non-integer, or a value larger than 2^32 - 1 to the length property results in a RangeError.
For example, extending an array:
Here, increasing pets.length to 4 extends the array with two empty slots, showcasing the dynamic nature of JavaScript arrays through the length property.
Examples
Iterating over an array
We can iterate over an array using length property in javascript.
Let's take an example in which we iterate over length of array in javascript and reduce each element by 1
Output:
Explanation: In the above code, we have assumed an array of elements 11,12,13,14,15 then we have assumed a variable named len which stores the array length in javascript using numbers.length property after that we have run a loop from 0 to len in which the loop in every iteration is decreasing the value of element by 1. At last after the loop completes we have printed the resulted array after the operation.
Time complexity: O(length of array)
Shortening an array
Output:
Explanation:
In the above code, we have assumed an array of elements 11,12,13,14,15, and then we have imposed a condition in which it will check the length of the array. If it is greater than 4, it will automatically change the array's length to 4. Else, it will not change and print the original array.
Create empty array of fixed length
Output:
Array with non-writable length
In JavaScript, the length property of an array is dynamically updated as new elements are added or removed, ensuring it always reflects the array's current size. However, if you set the length property to be non-writable, the array loses this flexibility, leading to potential issues when attempting to modify the array.
Example
Browser compatibility
Browser | Version | Supported |
---|---|---|
Safari | 1 | Yes |
Opera | 4 | Yes |
Internet Explorer | 4 | Yes |
Firefox | 1 | Yes |
Edge | 12 | Yes |
Chrome | 1 | Yes |
Conclusion
- The array length in JavaScript is a crucial 32-bit unsigned integer, consistently exceeding the array's highest index due to zero-based indexing.
- This length of array in JavaScript can extend up to 4,294,967,296 (2^32) elements, facilitating extensive array sizes.
- The array's length property can be changed to add or remove elements, making the array sparse.
- Using the length property, we can iterate over an array, shorten the length, and create an empty array of fixed length.