Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Add and Remove Elements in an Array Using Splice


    Learn how to use splice with one, two or three+ arguments. With one argument splice removes all elements past that index. With two arguments it removes the number of elements specified past the first argument index. With three+ arguments it removes the elements specified and adds all elements passed into the function.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    Instructor: Define an array. We'll remove elements from that array using Splice. We'll begin with using only the first argument, which is the index where removal should begin.

    As we can see, since we used index four, only the last element is removed. We'll use Splice with two arguments, the second being the number of elements past that index that should be removed.

    We'll use Splice with the first two arguments as zero, and all remaining arguments as elements to add to the array. Since we're removing nothing, only add will be empty, and array will have our new elements.

    We'll add and remove elements at the same time. We'll begin our removal at index two and remove three elements. Our additions will also begin at index two.

    As we can see, we have the elements in the middle of our array that were removed and our new elements with four at the end of the array, because they were inserted in the middle.