Skip to content

#220 Remove Null from an Array

Adam Kubiś edited this page Oct 28, 2026 · 1 revision

220 Remove Null from an Array

Create a function to remove all null values from an array.

Current source and tests | Catalog | Testing and tooling

Run this solution

From the cloned repository root, after installing dependencies:

node "Edabit/220 Remove Null from an Array.js"

Assertions are kept alongside the implementation and throw on failure. A successful run exits with code 0. Run every exercise with npm test or both lint and tests with npm run check.

Current implementation and tests

This is the complete executable file, including its imports and inline test cases. Run it from the repository, not as an isolated file copied out of the wiki. Line endings and trailing whitespace are normalized; executable tokens are unchanged.

//Create a function to remove all null values from an array.
function removeNull(arr) {
    return arr.filter(item => item !== null);
}
import { assertEquals } from '../testHelper.js';
assertEquals(removeNull(['a', null, 'b', null]), ['a', 'b'])
assertEquals(removeNull([null, null, null, null, null]), [])
assertEquals(removeNull([7, 8, null, 9]), [7, 8, 9])
assertEquals(removeNull([7, null, 8, null, 9]), [7, 8, 9])

Clone this wiki locally