- push() → Add element at end Adds a new element to the end of the array.[]
let fruits = ["apple", "banana"];
fruits.push("mango");
console.log(fruits);
👉 Output:
["apple", "banana", "mango"]
- pop() → Remove last element Removes the last item from the array.
JavaScript
let fruits = ["apple", "banana", "mango"];
fruits.pop();
console.log(fruits);
👉 Output:
["apple", "banana"]
✔️ Use when removing the latest item
🔹 3. shift() → Remove first element
Removes the first element.
JavaScript
let fruits = ["apple", "banana", "mango"];
fruits.shift();
console.log(fruits);
// ["banana", "mango"]
👉 Use when removing from start
🔹 4. unshift() → Add at beginning
Adds element to the start of array.
JavaScript
let fruits = ["banana", "mango"];
fruits.unshift("apple");
console.log(fruits);
// ["apple", "banana", "mango"]
👉 Use when adding at start
🔹 5. length → Find array size
Gives total number of elements.
JavaScript
let fruits = ["apple", "banana", "mango"];
console.log(fruits.length);
// 3
👉 Useful for loops and counting
🔹 6. includes() → Check value exists
Checks if a value is present in array.
JavaScript
let fruits = ["apple", "banana", "mango"];
console.log(fruits.includes("banana"));
// true
👉 Returns true or false
🔹 7. indexOf() → Find position
Returns index (position) of element.
JavaScript
let fruits = ["apple", "banana", "mango"];
console.log(fruits.indexOf("mango"));
// 2
👉 If not found → returns -1
🔹 8. join() → Convert array to string
Joins all elements into a string.
JavaScript
let fruits = ["apple", "banana", "mango"];
console.log(fruits.join(", "));
// "apple, banana, mango"
👉 Useful for displaying data
🔹 9. slice() → Get part of array
Returns a portion of array (does not change original).
JavaScript
let fruits = ["apple", "banana", "mango"];
console.log(fruits.slice(0, 2));
// ["apple", "banana"]
👉 Start index included, end index excluded
🔹 10. splice() → Add/Remove elements
Changes the original array.
JavaScript
let fruits = ["apple", "banana", "mango"];
fruits.splice(1, 1);
console.log(fruits);
// ["apple", "mango"]
👉 Syntax:
JavaScript
array.splice(start, deleteCount, newItem)
Top comments (0)