Posts

Showing posts from May, 2020

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"]

Truncate JavaScript array using length

Here are few examples to truncate JavaScript array using length property var chars = ["a", "w", "r", "t", "y", "y"]; console.log(chars); // ["a", "w", "r", "t", "y", "y"]; chars.length = 2; console.log(chars); // ["a", "w"]; chars.length = 0; console.log(chars); // [];