length property. Let’s go into detail.
Using array.length with elements have been assigned values
length property with the following syntax:
let arr1 = ["one", "two", "three"]; console.log(arr1.length); let arr2 = [1, 2, 3, 4, 5]; console.log(arr2.length); let arr3 = [9, ["five, six"], 1, 8]; console.log(arr3.length);
3 5 4
array.length will be calculated including both assigned elements and empty elements in the array.
Using array.length with empty elements in the array
For example, we have 5 elements in the below array. Although only 2 elements are used, the array’s length is not the number of elements used to assign the result but the total number of elements included. All empty elements are as follows:
let arr = new Array(5); arr = 2; arr = 8; // Display the length of the array console.log(arr.length); // Display the elements in the array console.log(arr);
5 [2, 8, null, null, null]
array.length has three properties: Writable, configurable, and enumerable.
- Writeable: By default, this property is set to true. By changing it to false, this property does not allow further modification to the length property.
- Configurable: This property is set to false by default. We cannot remove the property or change any more properties.
- Enumerable: By default, this property is set to false. If it is changed to true, then
array.lengthwill be iterated through the
Maybe you are interested:
Name of the university: HVNH BA
Major: htttql MIS