JavaScript Arrays


An array is a special variable, which can hold more than one value:

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

Why Use Arrays?

Suppose you have a collection of things, such as a array of car names. Storing these cars in individual variables would appear like this:

let car1 = "Saab";
let car2 = "Volvo";
let car3 = "BMW";

But, what if you need to go through the cars to locate a particular one? And what if you didn't have just three cars, but rather 300?

The solution is an array!

An array is like a container that can store multiple pieces of information using just one name. You can easily find and use each piece of information by knowing its unique index number.


Creating an Array

Creating a JavaScript Array is simplest when you use an array literal.

Syntax:

const array_name = [item1, item2, ...];      

It is a common practice to declare arrays with the const keyword.

Explore const and arrays in the chapter titled JS Array Const.

Spaces and line breaks don't matter. A statement can cover several lines:

You can make a array and put in the items like this:


Using the JavaScript Keyword new

The next example also makes a array and puts numbers in it:

The two examples above do exactly the same.

There is no need to use new Array().

For simplicity, readability and execution speed, use the array literal method.


Accessing Array Elements

To get a specific item from a array, you use the index number.:

Note : Array indexes start with 0.

[0] is the first element. [1] is the second element.


Changing an Array Element

This line of code modifies the value of the initial item within the cars collection:


Converting an Array to a String

The JavaScript function called toString() changes an array into a string where the values are separated by commas.


Access the Full Array

You can get the whole array in JavaScript by using the array's name.


Arrays are Objects

Arrays are like special things in programming. When you ask JavaScript what type of thing an array is, it says object.

Arrays are like arrays that use numbers to find their items. For instance, if you have a array called 'person,' accessing the first item would look like this: person[0], and it would give you the name 'John.'

Things have names to get their parts. In this instance, when you use person.firstName, it gives you "John".


Array Elements Can Be Objects

JavaScript variables can be objects. Arrays are special kinds of objects.

This means you can store different kinds of things in one group called an Array.

In an Array, you can put things. You can also put actions in an Array. Plus, you can even put more Arrays in an Array.

myArray[0] = Date.now;
myArray[1] = myFunction;
myArray[2] = myCars;

Array Properties and Methods

The true power of JavaScript arrays lies in their built-in array features and functions:

cars.length   // Returns the number of elements
cars.sort()   // Sorts the array

We will discuss array methods in the upcoming chapters.


The length Property

The length property of an array tells you how many things are in the array. It gives you the number of elements in the array.

The length property is always one more than the highest array index.


Accessing the First Array Element


Accessing the Last Array Element


Looping Array Elements

You can go through an array using a for loop like this:

You can also utilize the Array.forEach() function.


Adding Array Elements

You can easily add something new to a array by using the push() method:

You can put a new item into an array by using the length property.

WARNING !

Putting items into an array at big numbers can lead to empty spaces in the array.


Associative Arrays

Numerous coding languages allow the use of arrays that have specific names for each element.

Arrays that have names for their indexes are called associative arrays, or sometimes, hashes.

JavaScript does not allow arrays with named indexes.

In JavaScript, arrays always rely on numbered indexes.

WARNING !!
If you use named indexes, JavaScript will redefine the array to an object.

Following this, certain array functions and attributes may yield inaccurate outcomes.


The Difference Between Arrays and Objects

In JavaScript, arrays are like arrays, and they use numbers to keep track of their items.

In JavaScript, objects use named indexes.

Arrays are a unique type of objects that use numbers to organize their items.


When to Use Arrays. When to use Objects.

  • JavaScript does not support associative arrays.
  • You should use objects when you want the element names to be strings (text).
  • You should use arrays when you want the element names to be numbers.

JavaScript new Array()

JavaScript includes a built-in way to create an array using new Array().

But it's safe to use [] instead.

Both of these statements make a new, blank array called points.

These two statements both make a new array with 6 numbers:

The word "new" can lead to surprising outcomes in your code.


How to Recognize an Array

People often ask: "How can I tell if a variable is an array?"

The issue here is that when you use the JavaScript operator called typeof, it gives back the word object.

The typeof operator returns object because a JavaScript array is an object.

Solution 1:

To address this issue, ECMAScript 5, a version of JavaScript from 2009, introduced a new function called Array.isArray().

Solution 2:

The instanceof operator tells us if an object is made by a specific constructor.