JavaScript Arrays
JavaScript Tutorial » JavaScript Arrays
JavaScript arrays are used to store multiple values in a single variable.
JavaScript arrays can store any valid value, including strings, numbers, objects, functions, and even other arrays, thus making it possible to create more complex data structures such as an array of objects or an array of arrays.
Array:var cars = ["Audi", "Ferrari", "Ford", "Volvo", "BMW"];
document.getElementById("demo").innerHTML = cars;
</script>
What is an Array? - example
Let's suppose you want to store name of cars in your JavaScript code. Storing the name of cars one by one in a variable could look something like this:
var car2 = "Ferrari";
var car3 = "Ford";
var car4 = "Volvo";
var car5 = "BMW";
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 - example
Simple way to create an array in JS is enclosing a comma-separated list of values in square brackets ([]), as:
or: Array can also be created using the Array()
examples of arrays
<script>
// Creating variables
var colors = ["gree", "blue", "red"];
var cities = ["Madrid", "London", "Roma", "Paris"];
var person = ["Nicky", "Maldoran", 28];
// Printing variable values
document.write(colors + "<br>");
document.write(cities + "<br>");
document.write(person);
</script>
gree, blue, red
Madrid, London, Roma, Paris
Nicky, Maldoran, 28
Access the Elements of an Array
You can access an array element by referring to the index number as:<script>
var fruits = ["Avocado", "Cherry", "Lemon", "Pineapple", "Plum"];
document.write(fruits[0] + "<br>"); // Prints: Avocado
document.write(fruits[1] + "<br>"); // Prints: Cherry
document.write(fruits[2] + "<br>"); // Prints: Lemon
document.write(fruits[fruits.length - 1]); // Prints: Plum
</script>
Getting the Length of an Array, example
The length property returns the length of an array, which is the total number of elements contained in the array.
var my_fruits = ["Avocado", "Cherry", "Lemon", "Pineapple", "Plum", "banana"];
document.write(my_fruits.length); // Prints: 6
</script>
<script>
var now = new Date();
var dayOfWeek = now.getDay(); // Sunday - Saturday : 0 - 6
if(dayOfWeek == 5) {
alert("Have a nice weekend!");
} else if(dayOfWeek == 0) {
alert("Have a nice Sunday!");
} else {
alert("Have a nice day!");
}
</script>
Looping Through Array Elements - example
You can use for loop to access each element of an array in sequential order, like this:
<script>
var my_fruits = ["Avocado", "Cherry", "Lemon", "Pineapple", "Plum", "banana"];
// Iterates over array elements
for(var i = 0; i < my_fruits.length; i++){
document.write(my_fruits[i] + "<br>"); // Print array element
}
</script>
Avocado
Cherry
Lemon
Pineapple
Plum
banana
JavaScript Arrays methods, multidimensionales, asociativos, functions, contains, of objects, push, find, foreach, filter
JavaScript Arrays - javascript tutorial
This tool makes it easy to create, adjust, and experiment with custom colors for the web.

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.

Find here examples of creative and unique website layouts.

Find here examples of creative and unique website CSS HTML menu.