• 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 Check if Value is Not Undefined

Using Javascript to Check if Value is Not Undefined

August 30, 2022 Leave a Comment

We can easily use JavaScript to check if a value is not undefined by using the conditional operation not equal to, !=. Let’s see this below.

if( value1 != undefined ){
  //Value is not undefined
}

And here it is with a very simple example.

var value1 = 1; 
if( value1 != undefined ){
  console.log("value1 is not undefined");
}

#Output
value1 is not undefined

And it’s as simple as that. Now let’s see the not undefined check used in an example to remove undefined values from an array.


To remove undefined values from an array using JavaScript, one of the easiest ways to do this is to use a for loop with an if conditional statement. Here is a simple function we will create to remove undefined values from an array.

function removeUndefinedValues(arr){
  var new_array = [];
  for (var i=0; i<arr.length; i++){
    if( arr[i] != undefined ){
      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 undefined 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",undefined,"is","an","array",undefined,"with","undefined",undefined,"values","."]

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

console.log(removeUndefinedValues(array_of_strings));

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

Hopefully this article has been useful for you to learn how to use Javascript to check if a value is not undefined.

Other Articles You'll Also Like:

  • 1.  JavaScript cos – Find Cosine of Number in Radians Using Math.cos()
  • 2.  JavaScript join – Create String from Elements in an Array
  • 3.  Using JavaScript to Subtract Days From a Date
  • 4.  for each char in string – How to Loop Over Characters of String in JavaScript
  • 5.  Using JavaScript to Remove All Non-Alphanumeric Characters From a String
  • 6.  Using JavaScript to Get Today’s Date
  • 7.  Examples Using the JavaScript += Addition Assignment Operator
  • 8.  Remove Multiple Characters From String in JavaScript
  • 9.  Using JavaScript to Convert Month Number to Name
  • 10.  JavaScript toFixed – How to Convert a Number to a String

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