• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Remove Null Values From an Array

Using JavaScript to Remove Null Values From an Array

June 15, 2022 Leave a Comment

We can use JavaScript to remove null values from an array by making use of a for loop with an if conditional statement. Here is a simple function we will create to remove nulls from an array.

function removeNullValues(arr){
  var new_array = [];
  for (var i=0; i<arr.length; i++){
    if( arr[i] != null ){
      new_array.push(arr[i]);
    }
  }
  return new_array;
};

In the function above, we simply create a new array, loop through all the values of the old array, and only add values that are not null to the new array.

Notice above we also make use of the push() method. This simply adds the element to the end of the array.

And here is our function in use with an example array.


var array_of_strings = ["This",null,"is","an","array",null,"with","null",null,"values","."];

function removeNullValues(arr){
  var new_array = [];
  for (var i=0; i<arr.length; i++){
    if( arr[i] != null ){
      new_array.push(arr[i]);
    }
  }
  return new_array;
};

console.log(removeNullValues(array_of_strings));

#Output:
['This', 'is', 'an', 'array', 'with', 'null', 'values', '.']

When working with arrays of strings, it can be valuable to be able to easily filter and remove unwanted values from your array.

One such situation where you may want to remove values from an array is if you have a lot of null values in your array.

We can easily remove all nulls from an array using JavaScript by simply iterating over the array and removing the null values. This is done simply with the use of a for loop and an if conditional statement.

Below again is the code for our function which will remove all instances of null values from an array in JavaScript.

function removeNullValues(arr){
  var new_array = [];
  for (var i=0; i<arr.length; i++){
    if( arr[i] != null ){
      new_array.push(arr[i]);
    }
  }
  return new_array;
};

Using JavaScript to Remove Null Values From an Array Using the Filter Method

Below shows another way we can remove null values from an array, this time by using the filter() method.

var array_of_strings = ["This",null,"is","an","array",null,"with","null",null,"values","."]

var filtered_array = array_of_strings.filter(x => x != null);

Let’s put this code in a function to make it easier to remove null values from an array.

function removeNullValues(arr){
  return arr.filter(x => x != null);
};

And now let’s give it the same example as we did to our first function above.

function removeNullValues(arr){
  return arr.filter(x => x != null);
};

var array_of_strings = ["This",null,"is","an","array",null,"with","null",null,"values","."]

console.log(removeNullValues(array_of_strings));

// Output:
['This', 'is', 'an', 'array', 'with', 'null', 'values', '.']

Hopefully this article has been useful for you to learn how to use JavaScript to remove null from an array.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Round to 4 Decimal Places
  • 2.  Using JavaScript to Calculate Average of Array of Numbers
  • 3.  JavaScript isInteger – How to Check if a Number is an Integer
  • 4.  Using JavaScript to Get Substring Between Two Characters
  • 5.  Using JavaScript to Show a Div
  • 6.  Using JavaScript to Round to 2 Decimal Places
  • 7.  JavaScript atan2 – Find Arctangent of the Quotient of Two Numbers
  • 8.  Math floor JavaScript – Using Math.floor() to Round Down to Floor
  • 9.  JavaScript join – Create String from Elements in an Array
  • 10.  How to Clear a Textarea Field In JavaScript

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy