Remove falsy values from a JavaScript array using filter method

Here are examples of Falsy values (Falsy values returns false on evaluation) in JavaScript:

undefined , null , NaN , 0 , "",  false

We can use the JavaScript filter method to remove such values from our array

var valueArray = ["Nishant", "IT", false, "India", 0, NaN, "JavaScript", undefined, null];

console.log(valueArray.filter(Boolean)); // returns ["Nishant", "IT", "India", "JavaScript"]


Popular posts from this blog

Interview Question on Ruby on Rails

Inserting and Moving elements inside Ruby Array