How to delete a particular element from a JavaScript array?

JavascriptWeb DevelopmentFront End Technology

To delete an element from a JavaScript array, firstly use the indexOf() method to get the index of the element to be deleted. After that use splice() method −

Example

Live Demo

<html>    
   <body>              
      <script>  
         var array = [40, 20, 70, 50];        
         var index = array.indexOf(70);                
         document.write("Original array: "+array);  
         if (index > -1) {          
            array.splice(index, 1);      
         }  
         document.write("<br>New array after deletion: "+array);  
      </script>          
   </body>
</html>

Output

Original array: 40,20,70,50
New array after deletion: 40,20,50
raja
Published on 18-Feb-2018 10:47:41
Advertisements