Arrays have a property called length, and it's pretty much exactly as it sounds, it's the length of the array.

let array = [1, 2, 3];

let l = array.length;

// Result: l = 3

The length property also sets the number of elements in an array. For example.

let fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.length = 2;

// Result: ['Banana', 'Orange']

You can also use the length property to get the last element of an array by using it as an index. For example:

console.log(fruits[fruits.length - 1]); // Result: Orange

You can also use the length property to add elements to the end of an array. For example:

fruits[fruits.length] = "Pineapple";
console.log(fruits); // Result: ['Banana', 'Orange', 'Pineapple']

The length property is automatically updated when elements are added or removed from the array.

It's also worth noting that the length property is not a method, so you don't need to use parentheses when accessing it. It's simply a property of the array object that you can access like any other object property.

results matching ""

    No results matching ""