Pop Method


The pop() method removes the last element from an array and returns that element. This method is useful when you need to process elements from the end of an array one by one.


Example 1: Removing the last element

// Example of removing the last element from an array
const colors = ['red', 'green', 'blue'];
const lastColor = colors.pop();

console.log(colors);    // Output: ['red', 'green']
console.log(lastColor); // Output: 'blue'

Explanation

The pop() method is called on the colors array. It removes the last element, 'blue', from the array and returns it. The lastColor variable captures the removed element.


Example 2: Popping from an empty array

// Example of calling pop() on an empty array
const emptyArray = [];
const removedElement = emptyArray.pop();

console.log(emptyArray);    // Output: []
console.log(removedElement); // Output: undefined

Explanation

If you call pop() on an array that is already empty, the array remains unchanged, and the method returns undefined because there was no element to remove.


Example 3: Using pop() in a while loop

// Example of using pop() in a while loop to process an array
const tasks = ['Task 1', 'Task 2', 'Task 3'];
while (tasks.length > 0) {
  const currentTask = tasks.pop();
  console.log('Processing:', currentTask);
}
// Output:
// Processing: Task 3
// Processing: Task 2
// Processing: Task 1

Explanation

This example demonstrates a common pattern where pop() is used to iterate through and process an array's elements from last to first. The while loop continues as long as the array is not empty.


Example 4: Popping different data types

// Example of popping various data types
const mixedBag = [10, 'apple', true, { type: 'fruit' }];
const lastItem = mixedBag.pop();

console.log(mixedBag); // Output: [10, 'apple', true]
console.log(lastItem); // Output: { type: 'fruit' }

Explanation

Here, the mixedBag array contains different data types. The pop() method removes the last element, which is an object, and returns it, showcasing its ability to handle any data type.


Example 5: Chaining pop() calls

// Example of chaining pop() calls
const letters = ['a', 'b', 'c', 'd'];
const secondToLast = letters.pop();
const thirdToLast = letters.pop();

console.log(letters); // Output: ['a', 'b']
console.log(secondToLast); // Output: 'd'
console.log(thirdToLast); // Output: 'c'

Explanation

Each call to pop() modifies the array and returns the removed element. In this case, the first pop() removes 'd', and the second pop() removes 'c' from the now-modified array.


Example 6: Using the returned value from pop()

// Example of using the returned value from pop() in a condition
const stock = ['MSFT', 'GOOGL', 'AAPL'];
const lastStock = stock.pop();

if (lastStock === 'AAPL') {
  console.log('The last stock was Apple.'); // Output: The last stock was Apple.
}

Explanation

This example shows how the value returned by pop() can be immediately used. The removed element 'AAPL' is stored in lastStock and then used in a conditional if statement.


Example 7: Popping a nested array

// Example of popping a nested array
const data = [1, 2, ['a', 'b']];
const nestedArray = data.pop();

console.log(data);        // Output: [1, 2]
console.log(nestedArray); // Output: ['a', 'b']

Explanation

The data array contains a nested array as its last element. pop() removes this entire nested array as a single unit and returns it.