Skip to content

JavaScript Array

  • by

In JavaScript, an array is a data structure that holds a collection of elements, which can be of any data type, including other arrays. They allow for the storage and manipulation of data and are accessed using their index values.

const myArray = [element1, element2, ..., elementN];

This creates a new array with the given elements, separated by commas and enclosed in square brackets.

Access an element in an array using its index value within square brackets:

myArray[index];

To add elements to an array, you can use the push() method to add an element to the end of the array, or the unshift() method to add an element to the beginning of the array:

myArray.push(elementN+1);
myArray.unshift(element0);

You can remove elements from an array using the pop() method to remove and return the last element, or the shift() method to remove and return the first element:

const removedElement = myArray.pop();
const removedElement2 = myArray.shift();

If you want to iterate over an array, you can use the forEach() method to execute a function for each element in the array:

myArray.forEach(function(element) {
  // Do something with element
});

Use the sort() method to sort Array

myArray.sort();

To filter an array, you can use the filter() method to create a new array with only the elements that meet a certain condition:

const filteredArray = myArray.filter(function(element) {
  return element > 5;
});

JavaScript Array example

Simple example code includes creating, accessing, adding, and removing elements, iterating over arrays, sorting, and filtering.

<!DOCTYPE html>
<html>
<body>
    <script>
        // Create a new array
        const myArray = [1, "two", true, ["nested", "array"]];

        // Access an element
        console.log(myArray[0]);

        // Add a new element to the end of the array
        myArray.push("new element");
        console.log(myArray); 

        // Remove the first element from the array
        myArray.shift();
        console.log(myArray);

        // Iterate over the array and log each element
        myArray.forEach(function(element) {
            console.log(element);
        });


        // Create a new array with numbers
        const numArray = [3, 1, 4, 2];

        // Sort the array in ascending order
        numArray.sort();
        console.log(numArray);

        // Filter the array to get only the elements that are greater than 2
            const filteredArray = numArray.filter(function(element) {
            return element > 2;
        });
        console.log(filteredArray);

    </script>
</body>
</html>

Output:

JavaScript Array

JavaScript provides a variety of built-in methods that can be used to manipulate arrays. Here are some of the most commonly used array methods:

MethodDescription
concat()Merges two or more arrays into a new array
push()Adds one or more elements to the end of an array and returns the new length
pop()Removes the last element from an array and returns that element
shift()Removes the first element from an array and returns that element
unshift()Adds one or more elements to the beginning of an array and returns the new length
slice()Returns a new array containing a portion of an existing array
splice()Changes the contents of an array by removing or replacing existing elements and/or adding new elements
forEach()Executes a provided function once for each array element
map()Creates a new array with the results of calling a provided function on every element in the original array
filter()Creates a new array with all elements that pass the test implemented by the provided function
reduce()Applies a function to each element in an array to reduce the array to a single value
sort()Sorts the elements of an array in place according to a specified sorting order
reverse()Reverses the order of the elements in an array

Do comment if you have any doubts or suggestions on this JS array topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *