The length Property

The real strength of JavaScript arrays are the built-in array properties and methods, such as:

  var x = cars.length;   // The length property returns the number of elements in cars
  var y = cars.sort();   // The sort() method sort cars in alphabetical order

The length property of an array returns the length of an array (the number of array elements):

HTML file: Displayed by browser:
<body>
<h4>The length of the fruits array is:</h4>
<p id="demo"></p>
<script>
var fruits = [
    "Banana",
    "Orange",
    "Apple",
    "Mango"
];
document.getElementById("demo").innerHTML = fruits.length;
</script>
</body>

The length of the fruits array is:

The length property is always one more than the highest array index, which is mango at [3]. More about array properties and methods later in this tutorial.

BackTable of ContentsNext