Remove empty null undefined values from JavaScript Array


Remove empty null undefined values from JavaScript Array : There are many ways to remove the null values from the javascript arrays. Sometimes null/empty or undefined values are inserted in the array which are useless. So we need to remove the null, undefined values from array so that it remain good without garbage values. Here in this tutorial we are going to explain how you can remove these values from the javascript. You can try this with our online editor.


Remove empty null undefined values from JavaScript Array Example

JavaScript filter method to remove the null values from the array. If you want to remove all null values such as –

  • null
  • undefined
  • 0
  • ” “

You can use the filter method as below –

Remove empty null undefined values from JavaScript Array Example:

<html>
<head>
<title>Javascript Demo</title>
<script type='text/javascript'>
function cleanArray(){
   var array = ["Tutorialsplane",null, "0", "1", undefined, "9", "", "03"];
   array = array.filter(function(e){return e}); 
   document.getElementById('result').innerHTML = array;
}
</script>
</head>
<body>
array = ["Tutorialsplane",null, "0", "1", undefined, "9", "", "03"]; <br>
<a href="javascript:void(0)" onclick=" cleanArray()">Clean Array-</a><br>
Result array = <span id='result'></span>
</body>
</html>

Try it »

If you run the above example it will produce the output something like this –

Remove empty null undefined values from JavaScript Array Example


Advertisements

Add Comment

📖 Read More