How to replace an item in a JavaScript array

We may face this situation a lot in javascript development. So the below is the simply solution to replace an item in an array with splice() method.

// replace France to Italia
var countries = ['USA', 'France', 'England'];  
countries.splice(1, 1, 'Italia');  
// ["USA", "Italia", "England"]

You can find out more about array's method in this post.

Show Comments

Get the latest posts delivered right to your inbox.