JAVASCRIPT Tutorial

length in JS

Key Concepts:

  • Property: A characteristic of an object or data structure.
  • Read-only: A property that cannot be changed after it is initialized.
  • Dynamic: A property that changes dynamically as the object or data structure evolves.

'length' in JavaScript

'length' is a property of an array that indicates the number of elements it contains. It is a read-only property, meaning it cannot be changed directly. Instead, it updates automatically as elements are added or removed from the array.

Usage:

To access the 'length' property of an array, simply use the .length notation, as seen in the following example:

const fruits = ['apple', 'banana', 'orange'];
console.log(fruits.length); // Output: 3

Dynamic Nature:

The 'length' property is dynamic, meaning it changes whenever the array changes. This is illustrated in the following example:

fruits.push('pear'); // Add an element
console.log(fruits.length); // Output: 4

Summary:

  • 'length' is a read-only property of arrays that indicates the number of elements they contain.
  • It is a dynamic property that updates automatically as elements are added or removed from the array.
  • To access the 'length' property, use the .length notation.