Yahoo India Web Search

Search results

  1. Learn how to use the splice() method to add and/or remove array elements. See syntax, parameters, examples and browser support for this ECMAScript1 feature.

    • Overview
    • Syntax
    • Description
    • Examples
    • Browser compatibility
    • See also

    The splice() method of Array instances changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.

    To create a new array with a segment removed and/or replaced without mutating the original array, use toSpliced(). To access part of an array without modifying it, see slice().

    Parameters

    start Zero-based index at which to start changing the array, converted to an integer. •Negative index counts back from the end of the array — if -array.length <= start < 0, start + array.length is used. •If start < -array.length, 0 is used. •If start >= array.length, no element will be deleted, but the method will behave as an adding function, adding as many elements as provided. •If start is omitted (and splice() is called with no arguments), nothing is deleted. This is different from passing undefined, which is converted to 0.

    Return value

    An array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.

    The splice() method is a mutating method. It may change the content of this. If the specified number of elements to insert differs from the number of elements being removed, the array's length will be changed as well. At the same time, it uses @@species to create a new array instance to be returned.

    If the deleted portion is sparse, the array returned by splice() is sparse as well, with those corresponding indices being empty slots.

    Remove 0 (zero) elements before index 2, and insert "drum"
    Remove 0 (zero) elements before index 2, and insert "drum" and "guitar"
    Remove 0 (zero) elements at index 0, and insert "angel"
    splice(0, 0, ...elements) inserts elements at the start of the array like unshift().
    Remove 0 (zero) elements at last index, and insert "sturgeon"
    splice(array.length, 0, ...elements) inserts elements at the end of the array like push().

    BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

    •Indexed collections guide

    •Array

    •Array.prototype.concat()

    •Array.prototype.push()

    •Array.prototype.pop()

    •Array.prototype.shift()

  2. Jun 26, 2024 · Learn how to use the splice () method to modify an array by removing, replacing or adding elements. See syntax, examples and supported browsers for this JavaScript array method.

  3. People also ask

  4. Learn how to use the splice() method to manipulate arrays in JavaScript. See examples of deleting, inserting, and replacing elements in an array with the splice() method.

  5. Learn how to use the splice() method to modify an array by adding, removing or replacing elements. See syntax, parameters, return value and examples of splice() in JavaScript.

  6. Apr 23, 2021 · Learn how to use the splice() method to modify arrays in JavaScript. See examples of removing, adding, or replacing elements with splice() and its parameters.

  7. Jul 26, 2017 · The splice() method changes the contents of an array by removing existing elements and/or adding new elements. var myFish = ['angel', 'clown', 'mandarin', 'sturgeon']; myFish.splice(2, 0, 'drum'); // insert 'drum' at 2-index position.