Find Javascript Array Length

      No Comments on Find Javascript Array Length




length property of javascript array is used to find the length of an array or it can be used to set the length of an array.

e.g. array.length

Find length of a Javascript Array

length of an Javascript Array can be found simply by length property of an array.
The below demo javascript code explains it:

var numbers = [12,23,435,566,787,22,323];
var lengthOfArray = numbers.length;
console.log("Length of the array numbers = "+lengthOfArray);

Output of the above program in the console will be as below:

Length of the array numbers = 7

Set length of a Javascript Array

The length of a Javascript Array can be increased or decreased by dynamically setting the value of length property of an array.
If the length of array set is shorter than the original length then rest of the data is truncated and new length becomes the length of array.
If the length of array set is more than the original length then data remains intact and new length becomes the length of array.

var numbers = [12,23,435,566,787,22,323];
console.log("Original Length of the array numbers = "+numbers.length);
numbers.length = 2;
console.log("array numbers = "+numbers);
var lengthOfArray = numbers.length;
console.log("Modified Length of the array numbers = "+lengthOfArray);

Output of the above program in the console will be as below:
Original Length of the array numbers = 7
array numbers = 12,23
Modified Length of the array numbers = 2

Leave a Reply