JavaScript Arrays JAVASCRIPT
- JavaScript Introduction
- JavaScript Syntax
- JavaScript innerHTML
- JavaScript document.write()
- Javascript - window.alert()
- JavaScript - console.log()
- JavaScript Comments
- JavaScript Variables
- JavaScript Operators
- JavaScript Data Types
- JavaScript Functions
- JavaScript Objects
- JavaScript Events
- JavaScript Strings
- JavaScript String Methods
- JavaScript Numbers
- JavaScript Number Methods
- JavaScript Arrays
- JavaScript Array Methods
- JavaScript Sorting Arrays
- JavaScript Array Iteration
- JavaScript Date Objects
- JavaScript Date Formats
- JavaScript Get Date Methods
- JavaScript Set Date Methods
- JavaScript Math Object
- JavaScript Conditions
- JavaScript Switch
- JavaScript Loop For
- JavaScript While Loop
- JavaScript Break and Continue
- JavaScript Type Conversion
- JavaScript Errors
- JavaScript Scope
- JavaScript this Keyword
- JavaScript Classes
- JavaScript Debugging
- JavaScript - Changing CSS
- JavaScript JSON
JavaScript Arrays
JavaScript Arrays
JavaScript arrays are used to store multiple values in a single variable.
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:
var car2 = "Volvo";
var car3 = "BMW";
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?
The solution is an array!
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:
Spaces and line breaks are not important. A declaration can span multiple lines.
"Tesla",
"Volvo",
"BMW"
];
Using the JavaScript Keyword new
The following example also creates an Array, and assigns values to it.
Access the Elements of an Array
You access an array element by referring to the index number.
This statement accesses the value of the first element in cars.
Changing an Array Element
This statement changes the value of the first element in cars
:
Example
var cars = ["Tesla", "Volvo", "BMW"];
cars[1] = "TATA";
document.getElementById("demo").innerHTML = cars;
The length Property
The length
property of an array returns the length of an array (the number of array elements). The length
property is always one more than the highest array index. Because array indexing starts from 0.
Example
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.length; // the length of fruits is 4
Accessing the Last Array Element
The length
property is always one more than the highest array index. So get the last element by subtracting 1 in it.
Looping Array Elements
The safest way to loop through an array is by 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>";
}
text += "</ul>";
Adding Array Elements
The easiest way to add a new element to an array is by using the push()
method:
Example
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Lemon"); // adds a new element (Lemon) to fruits
New element can also be added to an array using the length
property:
Example
fruits[fruits.length] = "Lemon"; // adds a new element (Lemon) to fruits
WARNING !
Adding elements with high indexes can create undefined "holes" in an array:
Example
fruits[6] = "Lemon"; // adds a new element (Lemon) to fruits
Associative Arrays
Many programming languages support arrays with named indexes. Arrays with named indexes are called associative arrays (or hashes). JavaScript does not support arrays with named indexes.
In JavaScript, arrays always use numbered indexes.
Example
var person = [];
person[0] = "Deepak";
person[1] = "Chahar";
person[2] = 46;
var x = person.length; // person.length will return 3
var y = person[0]; // person[0] will return "John"