How to remove element from an Array in JavaScript?

Share Me
  •  
  •  
  •  
  • 2
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    2
    Shares
How to remove element from an Array in JavaScript

Hi Dev’s, In this blog, I will give you a example of how to remove element of array in javascript. Now here we will know how can remove array elements in different ways by using javascript. Here we know How to remove element from an Array in JavaScript.

So we can remove any elements from the beginning of an array by using shift(), from the end of array by using pop(), or from the particular and multiple by using splice().

So we can say it is very simple way to remove a specific element from an array as bellow example.

Example : Remove elements from the beginning of an array

shift() method is used to remove element from the beginning of an array in javascript.

<!DOCTYPE html>
<html>
<head>
    <title>Remove from the beginning of Array </title>
</head>
<body>
    <script type="text/javascript">

        var myArray = ['abc','xyz','def','ghi','jkl','mno'];	

        console.log(myArray);

        myArray.shift() //remove first element of array

        console.log(myArray);

    </script>
</body>
</html>
Example : Remove elements from the end of an array

The pop() method is used to remove element from the end of an array in javascript.

<!DOCTYPE html>
<html>
<head>
    <title>Remove from the end of Array </title>
</head>
<body>
    <script type="text/javascript">

        var myArray = ['abc','xyz','def','ghi','jkl','mno'];	

        console.log(myArray);

        myArray.pop() //remove last element of array

        console.log(myArray);

    </script>
</body>
</html>
Example : Remove specific elements of an array

The splice() method is used for remove particulate element of an array in javascript.

<!DOCTYPE html>
<html>
<head>
    <title>Remove specific elements of Array</title>
</head>
<body>
    <script type="text/javascript">

        var myArray = ['abc','xyz','def','ghi','jkl','mno'];	

        console.log(myArray);

        //Index start with zero.

        myArray.splice(2,1) //remove perticuler element of array
 	       
        console.log(myArray);

    </script>
</body>
</html>
Example : Remove all elements of an array

The splice() method is used to remove all element of an array in javascript.

<!DOCTYPE html>
<html>
<head>
    <title>Remove all elements of Array </title>
</head>
<body>
    <script type="text/javascript">

        var myArray = ['abc','xyz','def','ghi','jkl','mno'];	

        console.log(myArray);

        //Index start with zero.

        myArray.splice(0,myArray.length) //remove all element of array
 	       
        console.log(myArray);
		
    </script>
</body>
</html>
Example : Remove multiple elements of an array

The filter() function is used to remove multiple element of an array in javascript.

<!DOCTYPE html>
<html>
<head>
    <title>Remove multiple elements of Array </title>
</head>
<body>
    <script type="text/javascript">

        var ar = [1,2,3,4,5,6,7,8,9,0];

        console.log(ar);

        var filtered = ar.filter(function(value){

            return value > 5;

        });

        console.log(filtered);

    </script>
</body>
</html>

Read Also : Add Charts Using Highcharts In Laravel 8

Thanks for read. I hope it help you. For more you can follow us on facebook.

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →