push()
Adds one or more elements to the end of an array.
Example:
let fruits = ['Apple', 'Banana'];
fruits.push('Mango');
console.log(fruits); // Output: ['Apple', 'Banana', 'Mango']
pop()
Removes the last element from an array.
Example:
let fruits = ['Apple', 'Banana', 'Mango'];
let last = fruits.pop();
console.log(fruits); // Output: ['Apple', 'Banana']
console.log(last); // Output: 'Mango'
shift()
Removes the first element from an array.
Example:
let fruits = ['Apple', 'Banana', 'Mango'];
let first = fruits.shift();
console.log(fruits); // Output: ['Banana', 'Mango']
console.log(first); // Output: 'Apple'
unshift()
Adds one or more elements to the beginning of an array.
Example:
let fruits = ['Banana', 'Mango'];
fruits.unshift('Apple');
console.log(fruits); // Output: ['Apple', 'Banana', 'Mango']
splice()
Changes the contents of an array by removing or replacing existing elements and/or adding new elements.
Example:
let fruits = ['Apple', 'Banana', 'Mango', 'Orange'];
let removed = fruits.splice(2, 1, 'Kiwi');
console.log(fruits); // Output: ['Apple', 'Banana', 'Kiwi', 'Orange']
console.log(removed); // Output: ['Mango']