Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Remove Empty Elements from JavaScript Array</title> </head> <body> <script> // Sample array var arr = [1,2,,3,,-4,"",null,,0,,false,undefined,5,,-5,6,"",7,,]; // Defining a custom filter function function myFilter(elm){ return (elm != null && elm !== false && elm !== ""); } // Performing filtration var filtered = arr.filter(myFilter); document.write(JSON.stringify(filtered)); // Prints: [1, 2, 3, -4, 0, 5, -5, 6, 7] </script> </body> </html>