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

Common Array Methods in JavaScript


JavaScript provides several powerful methods for manipulating arrays. These methods make it easier to add, remove, and transform data within arrays. In this article, we will cover some of the most commonly used array methods: push, pop, shift, unshift, map, filter, and reduce. Examples are included to demonstrate how each method works.

1. push()

The push() method adds one or more elements to the end of an array and returns the new length of the array.

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

In this example, cherry is added to the end of the fruits array.

2. pop()

The pop() method removes the last element from an array and returns that element. If the array is empty, it returns undefined.

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

Here, pop() removes cherry from the fruits array and returns it.

3. shift()

The shift() method removes the first element from an array and returns that element. If the array is empty, it returns undefined.

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

In this example, shift() removes apple from the beginning of the array.

4. unshift()

The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.

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

Here, unshift() adds apple to the beginning of the fruits array.

5. map()

The map() method creates a new array by applying a function to each element in the original array. It does not modify the original array.

          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, map() doubles each element in the numbers array and returns a new array with the results.

6. filter()

The filter() method creates a new array containing elements that satisfy a specified condition. It does not modify the original array.

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

Here, filter() returns a new array with only the even numbers from the numbers array.

7. reduce()

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

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

In this example, reduce() calculates the sum of all elements in the numbers array.

Conclusion

Understanding these common array methods is essential for working with JavaScript arrays effectively. Each method has specific use cases and can help you manipulate data efficiently. By using methods like push, pop, shift, unshift, map, filter, and reduce, you can perform a wide range of operations on arrays, making JavaScript development more productive and enjoyable.





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