
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Filter null from an array in JavaScript?
To filter null from an array and display only non-null values instead, use filter(). Following is the code −
Example
var names=[null,"John",null,"David","","Mike",null,undefined,"Bob","Adam",null,null]; console.log("Before filter null="); console.log(names); var filterNull=[]; filterNullValues=names.filter(obj=>obj); console.log("After filtering the null values="); console.log(filterNullValues);
To run the above program, you need to use the following command −
node fileName.js.
Output
Here, my file name is demo148.js. This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo148.js Before filter null=[ null, 'John', null, 'David', '', 'Mike', null, undefined, 'Bob', 'Adam', null, null ] After filtering the null values= [ 'John', 'David', 'Mike', 'Bob', 'Adam' ]
- Related Articles
- Filter away object in array with null values JavaScript
- How to filter an array from all elements of another array – JavaScript?
- JavaScript in filter an associative array with another array
- Remove elements from array using JavaScript filter - JavaScript
- JavaScript: How to filter out Non-Unique Values from an Array?
- Remove/ filter duplicate records from array - JavaScript?
- Filter an object based on an array JavaScript
- How to filter values from an array using the comparator function in JavaScript?
- Filter array with filter() and includes() in JavaScript
- How to filter values from an array in TypeScript?
- Filter an array containing objects based on another array containing objects in JavaScript
- Filter array based on another array in JavaScript
- JavaScript filter an array of strings, matching case insensitive substring?
- Filter one array with another array - JavaScript
- Sorting an array objects by property having null value in JavaScript

Advertisements