JavaScript Array Tutorial Examples

JavaScript arrays are a powerful data structure that allows you to store and manipulate a collection of items. In this article we will explore the arrays and also the most common methods used on arrays.

JavaScript Array Examples

Here are a few examples of how you can work with arrays in JavaScript:

Creating an Array

To create an array, you can use the Array constructor or the array literal notation. For example:


let emptyArray = new Array();
let arrayWithItems = new Array(1, 2, 3);
let arrayWithItems2 = [1, 2, 3];

Accessing Items in an Array

You can access items in an array using the array index, which starts at 0. For example:


let myArray = [1, 2, 3, 4, 5];
console.log(myArray[0]); // output: 1
console.log(myArray[2]); // output: 3

Modifying Items in an Array

You can modify items in an array by reassigning their values using the array index. For example:


let myArray = [1, 2, 3, 4, 5];
myArray[1] = "hello";
console.log(myArray); // output: [1, "hello", 3, 4, 5]

Adding and Removing Items from an Array

You can add items to an array by using the push() method or by assigning a value to a new index. And use shift() and pop() method to remove the first and last elements of an array respectively.


let myArray = [1, 2, 3, 4, 5];
myArray.push(6);
console.log(myArray); // output: [1, 2, 3, 4, 5, 6]
myArray.shift();
console.log(myArray); // output: [2, 3, 4, 5, 6]
myArray.pop();
console.log(myArray); // output: [2, 3, 4, 5]

Iterating Over an Array

You can use a for-of loop or forEach() method to iterate over the items in an array. For example:


let myArray = [1, 2, 3, 4, 5];
for (let item of myArray) {
  console.log(item);
}
myArray.forEach(function(item) {
    console.log(item);
});

These are just a few examples of what you can do with arrays in JavaScript, and there are many other built-in methods and properties that you can use to work with arrays.

Javascript Array Methods

JavaScript provides a wide range of built-in methods for working with arrays. Here are some common array methods you might use in your code:

Concat Method

This method returns a new array that contains the elements of the original array, concatenated with the elements of one or more additional arrays or values.


let myArray = [1, 2, 3];
let myOtherArray = [4, 5, 6];
let combinedArray = myArray.concat(myOtherArray);
console.log(combinedArray); // output: [1, 2, 3, 4, 5, 6]

Join Method

This method returns a string that contains all the elements of an array, separated by a specified separator.


let myArray = [1, 2, 3, 4, 5];
console.log(myArray.join()); // output: "1,2,3,4,5"
console.log(myArray.join("-")); // output: "1-2-3-4-5"

Slice Method

This method returns a new array that contains a portion of the original array. The slice method takes 2 arguments, starting index and ending index. It returns the selected element in a new array.


let myArray = [1, 2, 3, 4, 5];
console.log(myArray.slice(1,3)); // output: [2, 3]

Map Method

This method creates a new array with the results of calling a provided function on every element in the calling array.


let myArray = [1, 2, 3, 4, 5];
let myNewArray = myArray.map(x => x * 2);
console.log(myNewArray); // output: [2, 4, 6, 8, 10]

Filter Method

This method creates a new array with all elements that pass the test implemented by the provided function.


let myArray = [1, 2, 3, 4, 5];
let filteredArray = myArray.filter(x => x > 3);
console.log(filteredArray); // output: [4, 5]

Sort Method

This method sorts the elements of an array in place and returns the sorted array.


let myArray = [5, 3, 2, 4, 1];
let sortedArray = myArray.sort();
console.log(sortedArray); // output: [1, 2, 3, 4, 5]

Reduce Method

This method applies a function to each element in the array and reduce it to single value.

let myArray = [1, 2, 3, 4, 5];
let sum = myArray.reduce((acc, current) => acc + current);
console.log(sum); // output: 15

These are some of the most commonly used array methods in JavaScript, but there are many more methods available, like push(), pop(), shift(), unshift(), indexOf() and so on.

It's important to keep in mind that some of these methods change the original array, while others return a new array, and that the behavior of the method can vary depending on the data type and number of elements in the array.