How to Check for Undefined in JavaScript

The typeof keyword returning "undefined" can mean one of two things: the variable is the primitive undefined, or the variable has not been declared at all. Here’s how to tell the difference.

What if a JavaScript value is undefined?

“The global undefined property represents the primitive value undefined. It is one of JavaScript's primitive types.” — MDN Docs

JavaScript variables start with the value of undefined if they are not given a value when they are declared (or initialized) using var, let, or const.

Variables that have not been declared usually cannot be referenced, except by using the typeof keyword, which will not throw a ReferenceError.

How to check for an undeclared variable?

“A variable that has not been assigned a value is of type undefined.

A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value.

A function returns undefined if a value was not returned.” — MDN Docs

Check out the following code about how to use undefined in JavaScript:

In the code snippet I also compared the behavior to checking for null in JavaScript, as it is similar but distinct to checking for undefined.

What’s the difference between null and undefined?

“Undefined is supposed to mean a variable has no value (or a property does not exist) because the programmer has not yet assigned it a value (or created the property).

Null is supposed to signal that a variable has no value because the programmer purposefully cleared the value and set it to `null`.” — Stephen Curtis on his Medium blog

That convention is not enforced by JavaScript, so it is often best to check for both null and undefined, as seen in the first code snippet.

Checking if a property exists on an object

But what if the variable in question is the property of a JavaScript object?

Undeclared properties of JavaScript objects always have the value undefined, whether or not those properties have been initialized.

Of course, that is assuming that the object itself has been declared.

The code example explains how to check for undeclared object properties:

As before, you may also want to check that object properties are not null while you are checking to make sure that they are not undefined.

The && one-liner works because undefined is falsy

I see that particular code pattern written commonly in React sites, where certain JSX elements are only included when certain props are present:

The above React component will only display the <div> containing the image if the property is found in the props passed to the component.

Conclusion

Technically, the value undefined is a primitive type in JavaScript, and it is a falsy value — meaning that it evaluates to false in Boolean conditionals.

This falsy property of undefined explains why some people use the && operator to write terse conditionals when checking for object properties.

The value undefined is loosely equal to null but not to other falsy values.

The typeof undefined or any undeclared variable is the string "undefined".

The difference between checking for undeclared variables and undeclared object properties is that undeclared variables usually result in a ReferenceError, except when using the typeof keyword.

Meanwhile, undeclared object properties always have the value undefined.

If the typeof a value is "undefined", then it is safe to say that the value is actually undefined — meaning it was not yet declared, declared but never assigned a value, or declared and assigned the value of undefined.

That will be true of both JavaScript variables and object properties.

Now you know how to check for undefined in JavaScript! 🔥👨‍💻💯👩‍💻🔥

Coding at Dawn

Laid-back programming + relaxing photography

Dr. Derek Austin 🥳

Written by

🤓 The physical therapist who writes JavaScript 💪 Web Developer 😎 Mentor 🧠 DPT 😄 SEO Expert 😁 React 😆 Jamstack 💬 Ask me anything 👉 DoctorDerek.com 👈

Coding at Dawn

Laid-back programming + relaxing photography

Dr. Derek Austin 🥳

Written by

🤓 The physical therapist who writes JavaScript 💪 Web Developer 😎 Mentor 🧠 DPT 😄 SEO Expert 😁 React 😆 Jamstack 💬 Ask me anything 👉 DoctorDerek.com 👈

Coding at Dawn

Laid-back programming + relaxing photography

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store