Javascript Array

In JavaScript, an array is a special variable that can hold multiple values at once. Unlike a regular variable, which can store just one value, an array lets you store a list of values, all of which can be accessed and modified easily.

Arrays are commonly used to manage collections of data, making them an essential part of programming.

What is an Array?

An array is like a container that holds multiple items in a specific order. Think of it as a shelf where each slot stores a piece of data—numbers, words, or even other arrays.

Arrays help organize information so you can easily access and manage it.

Creating Arrays

You can create an array in JavaScript in a few simple ways:

The first way to create an array is called Array literal.

Syntax

javascript
1
          let arrayName = [item1, item2, item3];
        
  • arrayName → The variable that holds the array.
  • [] → Square brackets define the array. We put inside the square brackets the itmes that we want to add to the array, Separate the items with a comma ,
  • item1, item2, item3 → itmes inside the array (can be numbers, strings, objects, etc.).

Example

javascript
1
2
3
          let fruits = ['apple', 'banana', 'orange'];

console.log(fruits);
        

fruits is the array name

"Apple", "Banana", and "orange" are items stored at indexes 0, 1, and 2.

The second way to create an array is called Array constructor.

The Array Constructor is another way to create arrays in JavaScript, though it's less common than the literal [] syntax. It can be useful in specific cases, like creating arrays with predefined lengths.

Syntax

javascript
1
          let arrayName = new Array(item1, item2, item3);
        
  • new Array() → Creates a new array instance.
  • Inside the parentheses, you can pass comma-separated values to initialize the array.

Example

javascript
1
2
3
          let fruits = new Array("Apple", "Banana", "Mango");

console.log(fruits); // Output: ["Apple", "Banana", "Mango"]
        

Indexing and Accessing Values

In an array, each item is stored in a specific position called an index.

JavaScript arrays use zero-based indexing, meaning the first item in the array has an index of 0, the second item has an index of 1, and so on.

To access an element, simply reference the array name followed by the index in square brackets:

If we want to access the first element inside the array, we will write the name of the array, then place square brackets and write inside the square brackets the index number of the element we want to access (in this case, 0).

javascript
1
2
3
4
          let colors = ['red', 'blue', 'green'];
console.log(colors[0]); // Output: red
console.log(colors[1]); // Output: blue
console.log(colors[2]); // Output: green
        

Update array elements

You can also modify an array’s values by assigning a new value to a specific index:

javascript
1
2
3
4
5
          let colors = ['red', 'blue', 'green'];

colors[1] = 'yellow'; // Changes 'blue' to 'yellow'

console.log(colors); // Output: ['red', 'yellow', 'green']
        

If you try to access an index that doesn’t exist, you’ll get undefined:

javascript
1
2
3
          let colors = ['red', 'blue', 'green'];

console.log(colors[5]); // Output: undefined
        

Nested Array

An array can contain other arrays as its elements. These are called nested arrays.

javascript
1
          let students = ['John', 'Jeff', 'Jack', [James, Steven]];
        

In this example, we find that the array contains 4 elements. If we look at the fourth element, we find that it is an array containing two elements.

If we print the fourth element in console it will return the array

javascript
1
2
          let students = ['John', 'Jeff', 'Jack', [James, Steven]];
console.log(students[3]);  // Accessing the nested array, Output: [James, Steven]
        

Accessing items in a nested array

We can access a specific element inside the nested array. By adding other square brackets and adding the index number of the element we want to access.

javascript
1
2
          let students = ['John', 'Jeff', 'Jack', ['James', 'Steven']];
console.log(students[3][0]); // Accessing and first element inside the nested array, Output: James
        

In this example, I typed the name of the array and then added square brackets and wrote inside them the index number of the element I wanted to access. In this example, I typed index 3, which refers to the nested array. Then I added other square brackets and wrote inside them 0 to access the first element in the nested array.

Length property

This property returns the number of elements in the array.

javascript
1
2
          let friends = ['John', 'Jeff', 'Jack'];
console.log(friends.length); // Output: 3
        

Common Use Cases

Arrays are versatile and can be used in many situations, including:

1. Storing multiple items: Arrays are ideal when you need to store a collection of similar items, like a list of names, numbers, or objects.

javascript
1
          let students = ['John', 'Jane', 'Alex'];
        

2. Looping through items: You can use loops to process or display all items in an array.

javascript
1
2
3
4
          let numbers = [10, 20, 30, 40];
for (let i = 0; i < numbers.length; i++) {
  console.log(numbers[i]);
}
        

3. Manipulating data: Arrays have several built-in methods that make it easy to add, remove, or rearrange items. For example:

javascript
1
2
3
          let fruits = ['apple', 'banana', 'orange'];
fruits.push('grape'); // Adds 'grape' to the end
fruits.pop(); // Removes the last item ('grape')
        

4. Storing different types of data: Arrays in JavaScript can hold mixed types of data, such as strings, numbers, and even other arrays or objects.

javascript
1
2
          let person = ['John', 25, 'Developer'];
let multiDimensionalArray = [[1, 2], [3, 4], [5, 6]];
        

Arrays are a powerful tool in JavaScript for organizing and manipulating data, whether you're building lists, working with data from APIs, or handling user input.