JAVASCRIPT Tutorial

Shift

Concept:

  • Modifies the original array
  • Returns the removed element

Steps:

  1. Call the shift() method on the array.
  2. The first element of the array is removed.
  3. The removed element is returned as the result.

JavaScript Example:

const array = [1, 2, 3, 4, 5];
const removedElement = array.shift(); // 1

console.log(array); // [2, 3, 4, 5]
console.log(removedElement); // 1

Additional Notes:

  • If the array is empty, shift() returns undefined.
  • shift() is useful for removing the first element from an array when you need to access the removed element.
  • It is important to remember that shift() modifies the original array.