• Home
  • Categories
  • Video Tutorials
    • Angular 5
  • News
  • About us
  • Contact us
  • Login
test
Code4Developers

Code4Developers

Code4Developers
  • Home
  • Categories
  • Video Tutorials
    • Angular 5
  • News
  • About us
  • Contact us
  • Login
  • JavaScript

Slice or Splice Which one to Use?

  • Arif Khoja
  • December 5, 2017
  • 2 minute read
javascript
Total
0
Shares
0
0
0

In this article we will try to get a clear idea about Slice or Splice which one is better to use…

Slice

Slice is a method on the Array Prototype that you can use to extract a section of a array. Let’s say you want to remove the first two elements from a array. Then you could do something like this:

let myList = [0,1,2,3,4,5,6,7,8,9]
myList = myList.slice(2)

You could of course do the same with a combination of filters and map without using slice:

let myList = [0,1,2,3,4,5,6,7,8,9]
myList = myList.map((value, index) => index => 2 ? value : undefined).filter(val => val!==undefined)

There aren’t much to the slice method, but the key thing is that it doesn’t mutate the array you run it on, but rather it returns a new copy. It takes two arguments: begin and end. If you don’t give it a end it treats the end as to the end of the array. And you can give it a negative number of remove things at the end. For example, like below to remove the last three elements at the end; without doing some weird .length hacks or double reversing.

let myList = [0,1,2,3,4,5,6,7,8,9]
myList = myList.slice(0, -3)

Slice is a very versatile and useful method, when you need to modify a list based on position instead of content. And it much more elegant than some of the other solutions I have seen (and written) to get the same thing. Everything from something like my map + filter hack above to some more advanced uses for the old school for loop.

Splice

Splice is another method on the Array Prototype; it is in many ways Slice’s weird brother in law. The main difference is that splice mutates the array you run it on, while slice does not. I personally never use splice because mutation often lead to unintended circumstances.

The other thing about splice is that you can’t do all of what you can do with slice; for example the cool -1 tricks for removing stuff at the end. This is where it gets a little bit “weird”. You can use it to remove stuff from an array. It takes a start argument and a number argument, for example like below (removing the two first elements)

let myList = [0,1,2,3,4,5,6,7,8,9]
myList.splice(0, 2)

But you can also feed it any number of elements after the number argument which will be inserted where you removed the elements. Splice can be very useful, but be careful, because everything is just weird enough that it is very easy to do something you didn’t want to and cause some difficult to spot bugs.

Let me end with the key differences between slice and splice:

  • Splice mutates
  • Slice doesn’t mutate
  • Slice works with start and end position
  • Splice works with a start position and a “delete Count”.
  • Splice can also append elements.
Arif Khoja
Arif Khoja

Arif Khoja is a Developer. He is a Javascript Enthusiatic who loves logical programming and has first hand experience in building a cutting edge internet product using Angular. He is also an open source freak and keen about learning and sharing. He writes Javascript both frontend and backend. He loves learning and sharing tech all the time. He also has a hands on experience in SEO and writes articles about latest emerging technologies.

Views: 6,261

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on WhatsApp (Opens in new window) WhatsApp
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on X (Opens in new window) X
  • Click to share on Pinterest (Opens in new window) Pinterest
  • Click to email a link to a friend (Opens in new window) Email
  • Click to print (Opens in new window) Print

Like this:

Like Loading...

Related Posts

Total
0
Shares
Share 0
Tweet 0
Pin it 0
1 comment
  1. David Binner says:
    February 19, 2019 at 9:39 am

    Hi, Arif.

    Nice article. You make a comment about slice: “And it much more elegant than some of the other solutions I have seen . . . to some more advanced uses for the old school for loop.”

    In some cases, an old school for-loop does the job whereas slice does not. For example, say you pass an array into a function, and also create a dummy array in the function on which most of the calculations will be performed. (Can be primitives such as numbers.) If you later use slice to copy the contents of the dummy array into the original array, the results will not be transmitted back out of the function. For that, you still need a for-loop.

    Reply

Leave a ReplyCancel reply

Subscribe to Website via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Recent Posts
  • React Hooks Guide: Top Tips for Optimizing Performance in Your React Applications

    React Hooks Guide: Top Tips for Optimizing Performance in Your React Applications

    1 year ago
  • Demystifying JavaScript Tree Shaking: Boosting Performance and Reducing Bundle Size

    Demystifying JavaScript Tree Shaking: Boosting Performance and Reducing Bundle Size

    2 years ago
  • Unlocking the Power of React Hooks: A Comprehensive Guide with Examples

    Unlocking the Power of React Hooks: A Comprehensive Guide with Examples

    2 years ago
  • Celebrating a Decade of Phenomenal Growth: Insights and Reflections on 10 Years of Software Engineering

    Celebrating a Decade of Phenomenal Growth: Insights and Reflections on 10 Years of Software Engineering

    2 years ago
  • Angular Custom Elements: Creating Reusable Components with Angular

    Angular Custom Elements: Creating Reusable Components with Angular

    2 years ago
  • Connect Firebase Realtime NoSQL Database with Angular App from Scratch

    Connect Firebase Realtime NoSQL Database with Angular App from Scratch

    5 years ago
  • How to Build an Inclusive Esports Community

    How to Build an Inclusive Esports Community

    5 years ago
  • Best Digital Icebreakers

    Best Digital Icebreakers

    5 years ago
  • Email alerts when a docker container stopped in AWS ECS CLUSTER

    Email alerts when a docker container stopped in AWS ECS CLUSTER

    5 years ago
  • New Learning Models for Fall 2020

    New Learning Models for Fall 2020

    5 years ago
Subscribe to Website via Email

Enter your email address to subscribe to this website and receive notifications of new posts by email.

Featured Posts
  • javascript 1
    Spread syntax (three dots) in JavaScript
    • March 21, 2018
  • Angular 2
    Angular 6 CRUD – Part 1: Project Setup, Routing, Service
    • May 9, 2018
  • javascript 3
    Local Storage and Session Storage
    • May 22, 2017
  • Angular 4
    Angular 4 Project Structure
    • June 18, 2017
  • AWS 5
    Email alerts when a docker container stopped in AWS ECS CLUSTER
    • July 24, 2020
Code4Developers
Learning is never ending process

Input your search keywords and press Enter.

%d