
272 views
JavaScript array.length
The JavaScript array length property is used to get the number of elements in an array. It returns the length or the number of items in the array.
Here’s an example that demonstrates the usage of the length property:
JavaScript
const fruits = ['apple', 'banana', 'orange'];
console.log(fruits.length); // Output: 3In the example above, the length property is accessed on the fruits array, which contains three elements. The length property returns the value 3, indicating that the array has three items.
Keep in mind that the length property is not a method but a property of the array object. Therefore, you don’t need to use parentheses when accessing it.
You can also use the length property to dynamically determine the size of the array, add or remove elements, or loop over the array using its length as a condition in a loop.