JS-061

Beginner5 min~5 min read#array#splice#arrays
splice()
Modify an array from within
Goal
Learn to remove or insert elements within an array using splice().
Explanation
- splice() changes the original array.
- You can use it to remove elements.
- It can also insert new elements.
- It operates using indexes.
- splice() is one of the most powerful array methods.
Analogy
Imagine a list of students. You can remove a student from the middle of the list or insert a new student in their place.
Code
const fruits = [
"Apple",
"Banana",
"Orange"
];
fruits.splice(1, 1);
console.log(fruits);Result: ["Apple", "Orange"]
Common mistake
Beginners often confuse slice() with splice(). slice() creates a copy, while splice() changes the original array.
Remember
- ✦ splice() changes an array.
- ✦ It can remove elements.
- ✦ It can insert elements.
Quick Quiz
What does splice() do?