• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Miscellaneous / Delete, replace, and add items to an array at specific indexes

Delete, replace, and add items to an array at specific indexes

Delete, replace, and add items to an array at specific indexes

The Array.splice() method accepts three arguments: start, delete, and items.

The first, start, is the index of the item you want to modify in the array. It’s the only required argument.

The second, delete, is the number of items to delete from the array. If you omit this argument, the Array.splice() method will remove every item from the start index on. If you set it to 0, it won’t remove any items.

Finally, if you want to insert one or more items into the array, you can pass them in as additional arguments.

var sandwiches = ['turkey', 'tuna', 'ham', 'pb&j'];

// Remove "ham" from the array
// It has an index of 2, and we only want to remove 1 item
sandwiches.splice(2, 1);

// Add "italian" between "tuna" and and "ham"
// Our target index is 2, we want to remove 0 items, and add "italian"
sandwiches.splice(2, 0, 'italian');

// Replace "tuna" with "chicken salad"
// It has an index of 1, we want to remove 1 item, and add "chicken salad"
sandwiches.splice(1, 1, 'chicken salad');

Source

https://vanillajstoolkit.com/reference/arrays/array-splice/

Miscellaneous

Related Snippets:

  • Check if the given string is an absolute URL
  • How To Check If a Variable is a String in JavaScript
  • What JavaScript Closures Are and How They Work
  • How To Clone An Object In JavaScript

Primary Sidebar

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2025 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers