To remove or replace an element in an array in JavaScript , you can use the splice method as shown below.
How to remove or replace array element in JavaScript ?
To remove the array element , you can use the splice method and specify the index of the array to remove the array element. The second parameter determines the number of elements to remove.
<script type="text/javascript"> var players = new Array('Sachin Tendulkar', 'Virat Kohli', 'Dhoni'); players.splice(players.indexOf('Dhoni'),1)); console.log(players); </script>
![image How to remove or replace array element in JavaScript ?](https://abundantcode.com/wp-content/uploads/2016/06/image_thumb-1.png)
Replace the array element with another element using the same splice method but pass the third parameter to define the replacement element.
<script type="text/javascript"> var players = new Array('Sachin Tendulkar', 'Virat Kohli', 'Dhoni'); players.splice(players.indexOf('Dhoni'), 1,"Ganguly"); console.log(players); </script>
![image image](https://abundantcode.com/wp-content/uploads/2016/06/image_thumb-2.png)
Leave a Reply