#Introduction to Arrays

Example: x = [1, 2, 3]

In order to understand what an array is, you must understand that indexing in an array begins at 0. In other words, the first element will begin at “0”, the second element in an array will be put as “1”, and so on. Arrays are used to access elements. [^fn]

*Creating an Array* Using an array literal is the easiest way to create a JavaScript Array.

Syntax: You want to declare a variable of your desired array type.

Note: Before 2015, you were limited to only using var as the declaration of your variable. But now, you may use let and const. There are subtleties that differentiate between the three.

let/const/var array_name = [item1, item2, …];

Spaces and line breaks are not important. A declaration can span multiple lines:

Ex:

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

[^fn]

Access the Elements of an Array

You refer to an array element by referring to the index number.

This statement accesses the value of the first element in cars:

var name = cars[0]; 
This statement modifies the first element in cars: cars[0] = "Opel";

[^fn] Example

var cars = ["Saab", "Volvo", "BMW"]; document.getElementById("demo").innerHTML = cars[0];

[^fn]

Array Properties and Methods

The real strength of JavaScript arrays are the built-in array properties and methods:

Examples

var x = cars.length; // The length property returns the number of elements var y = cars.sort(); // The sort() method sorts arrays Array methods are covered in the next chapters. 

[^fn]

The length Property

The length property of an array returns the length of an array (the number of array elements).

Example

Js var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.length; 
// the length of fruits is 4 

[^fn]

Looping Array Elements

The best way to loop through an array, is using a “for” loop:

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>"; }

[^fn]

[^fn] March 13, 2018 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array