In JavaScript, the pop() method is used to remove the last element from an array and return that element. This method modifies the array directly.
Syntax:
array.pop()
- array: The array from which the last element will be removed.
Example:
let fruits = ['apple', 'banana', 'orange'];
let removedFruit = fruits.pop();
console.log(removedFruit); // Output: "orange"
console.log(fruits); // Output: ['apple', 'banana']
Explanation:
- In the example, pop() removes the last element ('orange') from the fruits array and returns it.
- After calling pop(), the original array is modified, and the last element is removed.
The pop() method provides a straightforward way to remove the last element from an array, making it useful for various data manipulation tasks in JavaScript.