Checking for null values in JavaScript, With Examples

Javascript Null Check

What is null in JavaScript, and how can you check if a value is equal to null? Read on to find out.

What is Null?

Null is a term with a special meaning in computing and computer programming. Null is not 0 (zero).

Null means that something has no value. Zero is a value (a number). An empty string has a value (though sometimes called a null string, they aren’t null).

A variable with a value of null is considered to be empty. It is not undefined – it has been defined and purposefully left empty or assigned an empty value.

 

Find out more about null from the Mozilla Developer docs here.

Null is Not Undefined

An undefined variable is a variable that has not been declared. It does not have a value, but it does not have a value of null – it doesn’t exist.

A variable with a null value is an empty container. If a variable is undefined, the container doesn’t even exist.

It’s Also Not NaN

NaN (Not a Number) is another special value in JavaScript – but it’s not null – find out more about it here.

Why is Null Useful?

Setting a variable to a value of null can be useful.

Consider a game that lets you build a rollercoaster:

  • The score is calculated by the number of people who ride the rollercoaster.
  • Empty seats aren’t undefined – the seat is there; it’s just not filled.
  • They could be assigned a value of null to indicate this.
  • Filled seats could have a value with the passenger name or an object describing the passenger.
  • Empty seats can then easily be found on the roller coaster by checking whether they have a value of null.
    • Filled seats could be found by checking that they have a value that is not null if we wanted to interact with the passenger.

So, null is useful when you need to have a variable that may or may not have a value but must be defined. For example, you don’t want a roller coaster with missing (undefined) seats – or you won’t be able to fill them later.

I think I’ve just about exhausted that analogy now.

Check for Null in JavaScript Example

If you’re still with me, here’s how to check for null in Javascript – I’ll do the explaining in the comments:

// Define a variable with a value of null
var myValue = null; 

// Check if variable is equal to null
if(myValue === null) {
    // It's null!
}

// Check if variable is not equal to null
if(myValue !== null) {
    // It's not null!
}

Note the use of === and !== when comparing values. It’s best practice to do this whenever possible as opposed to == and !=.

=== and !== check for both type and value equality, making sure your variables are truly equal and don’t just evaluate to the same value – otherwise, you may get unexpected results.

SHARE:
nv-author-image

Brad Morton

I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Reply

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