Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Creating and Manipulating Arrays in JavaScript


Arrays are fundamental data structures in JavaScript, allowing us to store and manipulate lists of data. In this article, we’ll explore how to create arrays, access their elements, and use various methods to manipulate array contents.

Creating Arrays

There are multiple ways to create an array in JavaScript. Here are two common methods:

Using Array Literals

An array literal is the simplest way to create an array. Here’s an example:

          let fruits = ["apple", "banana", "cherry"];
          console.log(fruits); // Output: ["apple", "banana", "cherry"]
      

In this example, we created an array called fruits containing three string elements.

Using the Array Constructor

We can also create an array using the Array constructor:

          let numbers = new Array(1, 2, 3, 4, 5);
          console.log(numbers); // Output: [1, 2, 3, 4, 5]
      

Here, we created an array called numbers containing five numeric elements.

Accessing Array Elements

We can access individual elements in an array using their index. JavaScript arrays are zero-indexed, meaning the first element has an index of 0. Here’s an example:

          console.log(fruits[0]); // Output: apple
          console.log(fruits[2]); // Output: cherry
      

In this example, fruits[0] accesses the first element, and fruits[2] accesses the third element.

Modifying Array Elements

Array elements can be modified by assigning a new value to a specific index:

          fruits[1] = "blueberry";
          console.log(fruits); // Output: ["apple", "blueberry", "cherry"]
      

Here, we changed the second element of the fruits array from "banana" to "blueberry".

Adding and Removing Elements

JavaScript provides several methods for adding and removing elements from arrays.

Adding Elements

We can add elements to an array using push and unshift:

          fruits.push("date");      // Adds to the end
          fruits.unshift("avocado"); // Adds to the beginning
          console.log(fruits); // Output: ["avocado", "apple", "blueberry", "cherry", "date"]
      

The push method adds an element to the end of the array, while unshift adds an element to the beginning.

Removing Elements

We can remove elements using pop and shift:

          fruits.pop();     // Removes the last element
          fruits.shift();   // Removes the first element
          console.log(fruits); // Output: ["apple", "blueberry", "cherry"]
      

The pop method removes the last element, and shift removes the first element.

Array Length

The length property returns the number of elements in an array:

          console.log(fruits.length); // Output: 3
      

This property is useful for looping through arrays or checking if an array has elements.

Looping Through Arrays

We can loop through arrays using for, forEach, or for...of loops. Here’s an example with forEach:

          fruits.forEach(function(fruit) {
              console.log(fruit);
          });
          // Output:
          // apple
          // blueberry
          // cherry
      

The forEach method executes a function for each element in the array.

Other Array Methods

JavaScript provides many array methods to manipulate and transform arrays. Here are some commonly used methods:

map

The map method creates a new array by applying a function to each element:

          let numbers = [1, 2, 3, 4];
          let doubled = numbers.map(function(num) {
              return num * 2;
          });
          console.log(doubled); // Output: [2, 4, 6, 8]
      

In this example, each element in numbers is doubled, and the result is stored in doubled.

filter

The filter method creates a new array with elements that pass a condition:

          let evenNumbers = numbers.filter(function(num) {
              return num % 2 === 0;
          });
          console.log(evenNumbers); // Output: [2, 4]
      

In this example, evenNumbers contains only the even numbers from numbers.

reduce

The reduce method applies a function against an accumulator and each element in the array to reduce it to a single value:

          let sum = numbers.reduce(function(total, num) {
              return total + num;
          }, 0);
          console.log(sum); // Output: 10
      

Here, reduce calculates the sum of all elements in numbers.

Conclusion

Arrays in JavaScript provide powerful tools for storing and manipulating lists of data. By using various methods like push, pop, map, and filter, you can work with arrays efficiently. These skills are essential for working with data in JavaScript applications.





Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java