How can I check for “undefined” in JavaScript?

If you ever touched JavaScript, you surely have seen:

$ Uncaught ReferenceError: abc is not defined

This is very common. It happens when you attempt to access a property or function on a non-existing object. In plain English, this means, something is missing. Your variable might be not set or not initialized.

Again, this is one of my note-for-myself type of article.

How to fix "Uncaught ReferenceError: abc is not defined" #

Usually, you want to know whether the variable hasn't been declared or has the value undefined. The typeof operator, which is guaranteed to return a string, can help:

if (typeof myVar !== 'undefined') {
// Variable "myVar" is set.

This should help. It worked for me. I hope this works for you.


Since you've made it this far, sharing this article on your favorite social media network would be highly appreciated 💖! For feedback, please ping me on Twitter.