JavaScript Arrays
JavaScript Arrays
w3schools.com LOG IN
JavaScript Arrays
❮ Previous Next ❯
Example
var cars = ["Saab", "Volvo", "BMW"];
Try it Yourself »
What is an Array?
An array is a special variable, which can hold more than one value at a time.
If you have a list of items (a list of car names, for example), storing the cars in single
variables could look like this:
However, what if you want to loop through the cars and find a specific one? And what if
you had not 3 cars, but 300?
https://www.w3schools.com/js/js_arrays.asp 1/19
1/16/2021 JavaScript Arrays
An array can hold many values under a single name, and you can access the values by
referring to an index number.
Creating an Array
Using an array literal is the easiest way to create a JavaScript Array.
Syntax:
Example
Try it Yourself »
Spaces and line breaks are not important. A declaration can span multiple lines:
Example
var cars = [
"Saab",
"Volvo",
"BMW"
];
Try it Yourself »
https://www.w3schools.com/js/js_arrays.asp 2/19
1/16/2021 JavaScript Arrays
OPEN
Example
var cars = new Array("Saab", "Volvo", "BMW");
Try it Yourself »
The two examples above do exactly the same. There is no need to use new Array() .
For simplicity, readability and execution speed, use the first one (the array literal
method).
https://www.w3schools.com/js/js_arrays.asp 3/19
1/16/2021 JavaScript Arrays
Example
var cars = ["Saab", "Volvo", "BMW"];
document.getElementById("demo").innerHTML = cars[0];
Try it Yourself »
cars[0] = "Opel";
Example
Try it Yourself »
https://www.w3schools.com/js/js_arrays.asp 4/19
1/16/2021 JavaScript Arrays
Example
var cars = ["Saab", "Volvo", "BMW"];
document.getElementById("demo").innerHTML = cars;
Try it Yourself »
Arrays use numbers to access its "elements". In this example, person[0] returns
John:
Array:
var person = ["John", "Doe", 46];
Try it Yourself »
Object:
https://www.w3schools.com/js/js_arrays.asp 5/19
1/16/2021 JavaScript Arrays
Try it Yourself »
Because of this, you can have variables of different types in the same Array.
You can have objects in an Array. You can have functions in an Array. You can have
arrays in an Array:
myArray[0] = Date.now;
myArray[1] = myFunction;
myArray[2] = myCars;
Examples
var x = cars.length; // The length property returns the number of elements
var y = cars.sort(); // The sort() method sorts arrays
https://www.w3schools.com/js/js_arrays.asp 6/19
1/16/2021 JavaScript Arrays
Example
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.length; // the length of fruits is 4
Try it Yourself »
The length property is always one more than the highest array index.
Example
fruits = ["Banana", "Orange", "Apple", "Mango"];
var first = fruits[0];
Try it Yourself »
Example
fruits = ["Banana", "Orange", "Apple", "Mango"];
var last = fruits[fruits.length - 1];
Try it Yourself »
https://www.w3schools.com/js/js_arrays.asp 7/19
1/16/2021 JavaScript Arrays
Example
var fruits, text, fLen, i;
fruits = ["Banana", "Orange", "Apple", "Mango"];
fLen = fruits.length;
text = "<ul>";
for (i = 0; i < fLen; i++) {
text += "<li>" + fruits[i] + "</li>";
}
text += "</ul>";
Try it Yourself »
Example
text = "<ul>";
fruits.forEach(myFunction);
text += "</ul>";
function myFunction(value) {
text += "<li>" + value + "</li>";
}
Try it Yourself »
https://www.w3schools.com/js/js_arrays.asp 8/19
1/16/2021 JavaScript Arrays
Example
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Lemon"); // adds a new element (Lemon) to fruits
Try it Yourself »
New element can also be added to an array using the length property:
Example
Try it Yourself »
WARNING !
Adding elements with high indexes can create undefined "holes" in an array:
Example
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits[6] = "Lemon"; // adds a new element (Lemon) to fruits
Try it Yourself »
https://www.w3schools.com/js/js_arrays.asp 9/19
1/16/2021 JavaScript Arrays
Associative Arrays
Many programming languages support arrays with named indexes.
Arrays with named indexes are called associative arrays (or hashes).
Example
var person = [];
person[0] = "John";
person[1] = "Doe";
person[2] = 46;
var x = person.length; // person.length will return 3
var y = person[0]; // person[0] will return "John"
Try it Yourself »
WARNING !!
If you use named indexes, JavaScript will redefine the array to a standard object.
After that, some array methods and properties will produce incorrect results.
Example:
https://www.w3schools.com/js/js_arrays.asp 10/19
1/16/2021 JavaScript Arrays
Try it Yourself »
Use [] instead.
These two different statements both create a new empty array named points:
These two different statements both create a new array containing 6 numbers:
https://www.w3schools.com/js/js_arrays.asp 11/19
1/16/2021 JavaScript Arrays
Try it Yourself »
The new keyword only complicates the code. It can also produce some unexpected
results:
var points = new Array(40, 100); // Creates an array with two elements (40
and 100)
Try it Yourself »
The problem is that the JavaScript operator typeof returns " object ":
Try it Yourself »
https://www.w3schools.com/js/js_arrays.asp 12/19
1/16/2021 JavaScript Arrays
Solution 1:
To solve this problem ECMAScript 5 defines a new method Array.isArray() :
Try it Yourself »
The problem with this solution is that ECMAScript 5 is not supported in older
browsers.
Solution 2:
To solve this problem you can create your own isArray() function:
function isArray(x) {
return x.constructor.toString().indexOf("Array") > -1;
}
Try it Yourself »
Or more precisely: it returns true if the object prototype contains the word "Array".
Solution 3:
The instanceof operator returns true if an object is created by a given constructor:
https://www.w3schools.com/js/js_arrays.asp 13/19