What is ‘undefined’ in JavaScript?

JavaScript Undefined

This short article will explain what ‘undefined’ means in JavaScript – as both a type and a variable value.

Creating a Variable with an undefined value

To create a variable with an undefined value, it simply needs to be declared with no assigned value, for example:

var myVariable;
console.log(myVariable);

If the above code is executed, undefined is logged as the value of myVariable as no value was assigned.

undefined is a Type of Variable

undefined is one of the primitive variable types in JavaScript.

A variable type describes what a variable can be used for (for example, variables of a number type can be used in arithmetic, string type variables can be joined and split with other strings).

undefined being its own type means that it can’t be confused with a string containing the word ‘undefined’ – it has no defined, usable value. It’s undefined in every way except that the variable was named.

The JavaScript instanceOf() operator can tell you what type a variable is an instance of.

undefined is a Value

undefined variables are also treated as values – the value can be compared to check whether the variable is equal to undefined. Note that the actual value of an undefined variable is NOT undefined – it does not contain a string with those words – it only displays those words when logged to tell you it’s undefined. There is no value.

Undefined is Not null

undefined is not null.

This is a pretty important distinction.

null is an empty value – and sometimes we deliberately set empty values deliberately (for example, if programming a school bus simulator we might have an empty seat – it would have a null value because there’s nobody in it, not because no value was given). undefined has no value but wasn’t (and shouldn’t be) explicitly set as empty.

Checking if a Variable is Undefined

There are several ways to check whether a variable is undefined.

By Value

As undefined can be treated as a variable value, boolean comparison can be used to check if a variable is undefined:

if (myVariable === undefined) {
    // Value is undefined
}

Using typeof

The typeof operator returns the name of the type or constructor of the variable and can be used to check if a value is undefined (as it’s also a variable type, as explained above):

if (typeof myVariable === 'undefined') {
    // Value is undefined
}

This is the best way to check if a variable is undefined as it will not throw an error if the variable has not been declared.

You Can Name a Variable ‘undefined’ But You Really Shouldn’t

Outside of the global scope, you can give your variables the name undefined, for example:

function myFunction(){
    let undefined  = 'hello';
}

Don’t do this – you’re just making your life unnecessarily difficult for yourself. Furthermore, it will get really confusing whenever you need to use that variable.

What Can You Do With a Variable With an undefined value?

You can’t do much. Of course, you can check whether it is undefined and then decide to do something with it if it isn’t – but as undefined doesn’t have a useful value, attempting to use it in arithmetic or other logic operations is pretty fruitless.

You can find out more about undefined values in the Mozilla JavaScript developer docs.

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 *