Array Methods
Soundarya SVS
Posted on August 20, 2024
In JavaScript, arrays come with several methods to manipulate their elements, including Push(), Pop(), Unshift(), and Shift().
-
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];
-
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];
-
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];
-
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
Posted on August 20, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.