/

HTML / CSS / JavaScript Tutorial

JavaScript Array property: length

[this page | pdf | back links]

The length property (for a JavaScript array) returns the length of the array. An empty array has length 0.

 

It has the following syntax:

 

array.length

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
table,th,tr,td {border: 1px solid black; border-collapse: collapse;}
</style>
</head>
<body>
<table>
<tr>
<th>Example</th>
<th>Resulting value of <code>x</code></th>
</tr>
<tr>
<td><code id="Example"></code></td>
<td><code id="Result"></code></td>
</tr>
</table>

<script>
var g = [1, 2, 5, 4];
document.getElementById("Example").innerHTML =
  'var g = [1, 2, 5, 4];<br>' +
  'var x = g.length;';
document.getElementById("Result").innerHTML = g.length;
</script>

</body>
</html>


NAVIGATION LINKS
Contents | Prev | Next | JavaScript Arrays


Desktop view | Switch to Mobile