Home >>JavaScript Array Reference >JavaScript Array length Property

JavaScript Array length Property

JavaScript Array length Property

JavaScript array length property is used to set or return the number of elements in an array.

Syntax:

•To set the array length.

array.length = number

•returns the length of array.

array.length

Browser Support

Property Chrome Edge Firefox Safari Opera
length Yes Yes Yes Yes Yes

Here is an example of Array length property:

<html>
<body>
<button onclick="myLength()">Click me</button>
<p id="length"></p>
<script>
function myLength()
{
  var len = ["red", "green", "white", "blue", "orange", "yellow"];
  document.getElementById("length").innerHTML = len.length;
}
</script>
</body>
</html>
Output:

 

Example 2:

<html>
<head>
<title> JavaScript Array length Property </title>
</head>
<body>
<h2>Phptpoint</h2>
<h3>
JavaScript Array length Property
</h3>
<button onclick="array()">
Click Here
</button>
<p id="php"></p>
<script>
function array()
{
var len1 = ["A", "B", "C","D", "E", "F","G" ];
document.getElementById("php").innerHTML= len1.length;
}
</script>
</body>
</html>
Output: