AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

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:

Example:
<script>
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:

Example:
var car1 = "Audi";
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:

Example: syntax
var my_Array = [element_0, element-1, ..., element_N];

or: Array can also be created using the Array()
var my_Array = new Array(element_0, element_1, ..., element_N);


examples of arrays

Example:

<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>

Output:
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:

Example:
<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.

Example:
<script>
var my_fruits = ["Avocado", "Cherry", "Lemon", "Pineapple", "Plum", "banana"];

document.write(my_fruits.length); // Prints: 6
</script>


Example: a is defined as a variable, and a is given the value 15:

<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:

Example:
    <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>
Output:
Avocado
Cherry
Lemon
Pineapple
Plum
banana




JavaScript Arrays methods, multidimensionales, asociativos, functions, contains, of objects, push, find, foreach, filter
JavaScript Arrays - javascript tutorial

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.