Array Methods

soundarya_svs_01d4781241c

Soundarya SVS

Posted on August 20, 2024

Array Methods

In JavaScript, arrays come with several methods to manipulate their elements, including Push(), Pop(), Unshift(), and Shift().

  1. Push() Method

    • Purpose: Adds one or more elements to the end of an array.
    • Returns: The new length of the array.
    • Example: let arr = [1, 2, 3]; arr.push(4); output: [1,2,3,4];
  2. Pop() Method

    • Purpose: Removes the last element from an array.
    • Returns: The removed element.
    • Example: let arr = [1, 2, 3, 4] let lastElement = arr.pop(); output: [1,2,3];
  3. Unshift() Method

    • Purpose: Adds one or more elements to the beginning of an array.
    • Returns: The new length of the array.
    • Example: let arr = [2, 3, 4]; arr.unshift(1); output: [1,2,3,4];
  4. Shift() Method

    • Purpose: Removes the first element from an array.
    • Returns: The removed element.
    • Example: let arr = [1, 2, 3, 4]; let firstElement = arr.shift(); output: [2,3,4];
💖 💪 🙅 🚩
soundarya_svs_01d4781241c
Soundarya SVS

Posted on August 20, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related