JavaScript Arrays

JavaScript arrays are written with square brackets. Array items are separated by commas. Array indexes are zero-based, which means the first item is [0], second is [1], and so on. The following code declares (creates) an array called cars, containing three items (car names) and the browser will display the first item [0]:

  var cars = ["Saab", "Volvo", "BMW"];

HTML file: Displayed by browser:
<body>
<p id="demo"></p>
<script>
   var cars = ["Saab", "Volvo", "BMW"];
   document.getElementById("demo").innerHTML = cars[0]
</script>
</body>

More about arrays later in this tutorial.

BackTable of ContentsNext