While writing code, we might need to store multiple values. We can use a data type named array to meet this need. An array is a variable type in any programming language used to store multiple values of the same data type, such as a list of students or employees [Jane, John, Jack].
Note: We will use the browser console to demonstrate examples performed in this post. To open up the browser console:
- Use the F12 key in Chrome and other chromium-based browsers.
- Use CTRL + SHIFT + K keyboard shortcut keys for Mozilla.
- Use Option + ⌘ + C keyboard shortcut keys in Safari (if developer menu does not appear, then open Preferences by pressing ⌘ +, and in Advanced tab check “Show Develop menu in menu bar”).
What is an array.length property in JavaScript
The array.length property is used to get the total number of elements in an array. For example, if we have the following array in our program:
And we use the array.length property on it, then the array.length property will return a value of three as the array contains three elements. We can verify this by using the console.log() method.
console.log(students.length)
Now, if we add another element to the array and use the array.length property again, then the output on the console changes to:
console.log(students.length)
How to set the number of elements in an array using the array.length method:
We can use the array.length property to set the number of elements in an array as well.
Let’s take the array as mentioned above, “students,” as an example once again. It currently has 4 elements. We can use the array.length property to change the number of its elements. We will increase the number of elements from 4 to 5 in this example:
students.length = 5;
console.log(students.length)
The array.length property has added another element which is a non-iterable empty slot. We can verify this by outputting the whole array to the console:
students.length = 5;
console.log(students.length)
console.log(students)
Now we will use the array.length property once again to reduce the number of elements from 5 to 3:
students.length = 5;
console.log(students.length)
console.log(students)
students.length = 3;
console.log(students.length)
console.log(students)
The maximum number of elements an array can have is 4294967295, as it is a 32-bit data type. As you can see in the example below, if we try to make an array of length4294967296, we will get an error.
console.log(students.length)
Conclusion
An array is a data structure consisting of a collection of elements that are used to store similar types of values. The array.length property is used to get or set the number of elements present in an array.
If we use this property to get the number of array elements, it will give a number one higher than the highest index of the array; This is because of array indexing which starts at 0.
In this how to guide, we have learned how to use the array.length property. This property really comes in handy when we have to run loops or conditionals on arrays.
from https://ift.tt/37PS1Pu
0 Comments