Splice and slice using Javascript

Hello Fellow Developers in this blog we are going to talk about a powerful array properties Splice and slice.

JavaScript arrays have many built-in methods that allow you to manipulate the data they contain. Two of the most commonly used methods for working with arrays are slice() and splice(). These methods can be used to extract or remove elements from an array, and are particularly useful when working with large datasets.

The slice() method is used to extract a section of an array, and creates a new array with the extracted elements. The method takes two arguments: the starting index and the ending index of the section you want to extract. The elements in the original array are not modified. Here is an example of using the slice() method to extract the middle elements of an array:

var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var middle = numbers.slice(3, 7);
console.log(middle); // [4, 5, 6, 7]

In this example, the slice() method is called on the “numbers” array, with the starting index of 3 and the ending index of 7. The resulting “middle” array contains the elements at index 3, 4, 5, and 6 of the original array.

The splice() method, on the other hand, is used to add or remove elements from an array. It takes three arguments: the starting index, the number of elements to remove, and the elements to add. The method modifies the original array and returns an array containing the removed elements. Here is an example of using the splice() method to remove the middle elements of an array:

var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var removed = numbers.splice(3, 4);
console.log(numbers); // [1, 2, 3, 8, 9, 10]
console.log(removed); // [4, 5, 6, 7]

In this example, the splice() method is called on the “numbers” array, with the starting index of 3 and the number of elements to remove 4. The resulting “removed” array contains the elements that were removed from the original array, and the original array is modified with those elements removed.

You can also use splice() method to add elements into an array.

var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var removed = numbers.splice(3, 2, 15, 16);
console.log(numbers); // [1, 2, 3, 15, 16, 5, 6, 7, 8, 9, 10]
console.log(removed); // [4, 5]

In this example, the splice() method is called on the “numbers” array, with the starting index of 3, the number of elements to remove 2 and add elements 15, 16. The resulting “removed” array contains the elements that were removed from the original array, and the original array is modified with those elements removed and new elements added.

In conclusion, both the slice() and splice() methods are useful for working with arrays in JavaScript. The slice() method can be used to extract a section of an array without modifying the original array, while the splice() method can be used to add or remove elements from an array and it modifies the original array. Understanding when to use each method is important for writing efficient and effective code.

Thanks for reading Please let me know if there are any doubts in comment section.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories