Articles → JAVASCRIPT → Splice Functions In Javascript
Splice Functions In Javascript
Purpose
Syntax
- The first parameter indicates the index of the array where the item can be added or removed.
- The second parameter is the count of the number of elements to be deleted. If the count is zero then it means that we are adding the value in an array.
- The third parameter is an array of items to be inserted.
Add An Element In An Array
<!DOCTYPE html>
<html lang="en">
<head>
<script>
const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb'); // => Inserts the value at index 1
months.splice(2, 0, 'Feb'); // => Inserts the value at index 2
months.splice(3, 0, 'Feb'); // => Inserts the value at index 3
console.log(months);
</script>
</head>
<body></body></html>
Removing Elements In An Array
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<script>
const months = ['Jan', 'March', 'April', 'June'];
months.splice(3, 1, 'May'); // => Delete
console.log(months);
</script>
</head>
<body></body></html>