Why JavaScript’s undefined Isn’t What You Think It Is

by Christoph Schiessl on JavaScript

All syntax highlighters I've ever worked with treat JavaScript's undefined as a keyword. However, that's a misconception.

// global scope
undefined = 123;   // assign to `undefined`
undefined === 123; // ==> evaluates to true

The code snippet above looks strange, but it's valid JavaScript. Pygments, the highlighter I'm using for this website, is misleading because it considers undefined a keyword. But in reality, there's nothing special about undefined:

  • Syntactically, it's an ordinary identifier (not a keyword).
  • Semantically, it's a property of the so-called Global Object.

I'm assuming you are using a more or less recent browser to read this article. If so, you, unfortunately, can't reproduce the above behavior because your browser implements the ECMAScript 5 (short: ES5) standard. ES5 introduces many changes, but one is particularly important in this context — namely, they updated the undefined property to be non-writeable.

In a nutshell, this means that the last line of the above snippet evaluates to...

  • ... true if you are using an old browser. For instance, Internet Explorer 8 implements the old behavior (i.e., writeable undefined).
  • ... false if you are using a recent browser. For instance, Google Chrome implements the new behavior (i.e., non-writeable undefined).

The non-writeable undefined in ES5 (or newer) mitigates the problem, but it's still easy to construct a situation where undefined is not what you expect. All it takes is a function scope and a var ...

(function () {
  // Remember: `undefined` is an identifier (not a keyword)
  var undefined = 'foobar';

  // Clearly, 'foobar' is defined (its type is 'string')
  if ('foobar' === undefined) {
    console.log('foobar is *UN*defined!!!');
  } else {
    console.log('foobar is defined!!!');
  }
})();
// Output: foobar is *UN*defined!!!

Let me conclude by pointing out that it is your responsibility as a JavaScript developer to ensure that undefined really refers to the value its name suggests. You are responsible for ensuring that typeof undefined === 'undefined' is always true.

Thank you for reading, and see you soon!

Web App Reverse Checklist

Ready to Build Your Next Web App?

Get my Web App Reverse Checklist first ...


Software Engineering is often driven by fashion, but swimming with the current is rarely the best choice. In addition to knowing what to do, it's equally important to know what not to do. And this is precisely what my free Web App Reverse Checklist will help you with.

Subscribe below to get your free copy of my Reverse Checklist delivered to your inbox. Afterward, you can expect one weekly email on building resilient Web Applications using Python, JavaScript, and PostgreSQL.

By the way, it goes without saying that I'm not sharing your email address with anyone, and you're free to unsubscribe at any time. No spam. No commitments. No questions asked.

Continue Reading?

Here are a few more Articles for you ...


Exploring Orphaned Branches to Understand Git's Internals

Learn about Git's internal data structure and how orphaned branches can be used to create separate histories with their own root commits.

By Christoph Schiessl on DevOps and Git

How to Recursively Create Directories

Creating directories in Python using the pathlib module, including normal and recursive creation, handling existing directories, and file system permissions.

By Christoph Schiessl on Python

How to <link> your Blog's Atom/RSS Feed from HTML Pages

Learn how to <link> Atom and RSS feeds from your HTML documents to make them discoverable for clients and, by extension, for your readers.

By Christoph Schiessl

Christoph Schiessl

Hi, I'm Christoph Schiessl.

I help you build robust and fast Web Applications.


I'm available for hire as a freelance web developer, so you can take advantage of my more than a decade of experience working on many projects across several industries. Most of my clients are building web-based SaaS applications in a B2B context and depend on my expertise in various capacities.

More often than not, my involvement includes hands-on development work using technologies like Python, JavaScript, and PostgreSQL. Furthermore, if you already have an established team, I can support you as a technical product manager with a passion for simplifying complex processes. Lastly, I'm an avid writer and educator who takes pride in breaking technical concepts down into the simplest possible terms.