JavaScript · JS Tutorial · Lesson 19
JS Arrays
Store multiple values in one variable.
An array is a special variable which can hold more than one value, accessed by an index number starting at 0.
Example
<p id="demo"></p>
<script>
const cars = ["Saab", "Volvo", "BMW"];
cars[0] = "Opel"; // change an element
cars.push("Audi"); // add to the end
let out = "First: " + cars[0] + "<br>";
out += "Last: " + cars[cars.length - 1] + "<br>";
out += "Count: " + cars.length + "<br>";
out += "Is array: " + Array.isArray(cars) + "<br><ul>";
cars.forEach(function (car) { out += "<li>" + car + "</li>"; });
document.getElementById("demo").innerHTML = out + "</ul>";
</script>
Tip: Arrays use numbered indexes; objects use named keys. Use
Array.isArray() to check for an array, because typeof returns "object".Keep reading “JS Arrays”
Sign up to read the full lesson, try every example in the live editor, and take the 5-question quiz.