Javascript array length
property sets or gets the number of elements in array.
The value of array length
is an unsigned, 32-bit integer.
const arr = [1, 2];//from w w w . ja v a 2s. com console.log(arr);// [ 1, 2 ] arr.length = 5; // set array length to 5 while currently 2. console.log(arr); arr.forEach(element => console.log(element));
Return the length of an array:
var languages = ["CSS", "HTML", "Java", "Javascript"]; console.log(languages.length);
The array numbers is iterated through by looking at the length property.
The value in each element is then doubled.
var numbers = [1, 2, 3, 4, 5]; var length = numbers.length; for (var i = 0; i < length; i++) { numbers[i] *= 2;/*from w ww .jav a 2 s . c om*/ } // numbers is now [2, 4, 6, 8, 10]
Shortening an array. The following example shortens the array numbers to a length of 3.
var numbers = [1, 2, 3, 4, 5]; if (numbers.length > 3) { numbers.length = 3;/*from w w w.j a v a 2 s . c o m*/ } console.log(numbers); // [1, 2, 3] console.log(numbers.length); // 3
Create empty array of fixed length
var numbers = []; numbers.length = 3; console.log(numbers); // [undefined, undefined, undefined]
The number of items in an array is stored in the length property:
let colors = ["red", "blue", "green"]; // creates an array with three strings let names = []; // creates an empty array console.log(colors.length); // 3 console.log(names.length); // 0
Array length is not read-only.
By setting the length property, we can remove items from or add items to the end of the array.
let colors = ["red", "blue", "green"]; // creates an array with three strings console.log(colors); /*from w ww .j a va2s .co m*/ colors.length = 2; console.log(colors); console.log(colors[2]); // undefined
If the length were set to a number greater than the length
, the new items would each get filled with the value of undefined:
let colors = ["red", "blue", "green"]; // creates an array with three strings colors.length = 4; //from w w w . j a v a 2 s .com console.log(colors[3]); // undefined
The length property can add items to the end of an array:
let colors = ["red", "blue", "green"]; // creates an array with three strings console.log(colors);// www . j a va 2 s .c o m colors[colors.length] = "black"; // add a color (position 3) console.log(colors); colors[colors.length] = "brown"; // add another color (position 4) console.log(colors);
The last item in an array is always at position length - 1.
The new length is calculated when an item is placed into a position that's outside of the current array size.
let colors = ["red", "blue", "green"]; // creates an array with three strings colors[99] = "black"; // add a color (position 99) console.log(colors.length); // 100
The following example creates an array, msgArray
, with a length of 0.
Then assigns values to msgArray[0]
and msgArray[99]
, changing the length of the array to 100.
let msgArray = [] msgArray[0] = 'Hello' msgArray[99] = 'world' if (msgArray.length === 100) { console.log('The length is 100.') }
Arrays can contain a maximum of 4,294,967,295 items.