JavaScript Array Methods
JavaScript provides a variety of built-in methods for working with arrays. These methods allow you to manipulate array elements, iterate over arrays, and perform various operations efficiently.
1. push()
Adds one or more elements to the end of an array and returns the new length of the array.
Example:
const fruits = ["Apple", "Banana"];
fruits.push("Orange", "Mango");
console.log(fruits); // Output: ["Apple", "Banana", "Orange", "Mango"]
2. pop()
Removes the last element from an array and returns that element.
Example:
const fruits = ["Apple", "Banana", "Orange"];
const removedFruit = fruits.pop();
console.log(removedFruit); // Output: "Orange"
3. shift()
Removes the first element from an array and returns that element.
Example:
const fruits = ["Apple", "Banana", "Orange"];
const removedFruit = fruits.shift();
console.log(removedFruit); // Output: "Apple"
4. unshift()
Adds one or more elements to the beginning of an array and returns the new length of the array.
Example:
const fruits = ["Banana", "Orange"];
const newLength = fruits.unshift("Apple", "Mango");
console.log(fruits); // Output: ["Apple", "Mango", "Banana", "Orange"]
These are just a few examples of JavaScript array methods. There are many more methods available for various array operations, such as concatenating arrays, slicing arrays, searching for elements, and more.