Skip to content

Instantly share code, notes, and snippets.

@trevor-atlas
Created May 24, 2019 00:22
Show Gist options
  • Save trevor-atlas/756abaddb1f3fdc5a0e89e195c7ba188 to your computer and use it in GitHub Desktop.
Save trevor-atlas/756abaddb1f3fdc5a0e89e195c7ba188 to your computer and use it in GitHub Desktop.

In JavaScript, undefined is nothing but a global variable name without a default value. Therefore, its primitive value is undefined. You can change the value of undefined:

const a = {};
a.b === undefined; // true because property b is not set
undefined = 42;
a.b === undefined; // false

Due to the mutability of undefined, it is generally a better idea to check for undefined-ness through typeof or void:

const a = {};
typeof a.b == 'undefined'; // always true
void 0 // always evaluates to undefined
@trevor-atlas
Copy link
Author

Note that this is not the case in modern execution environments, so this probably doesn't matter as much any longer. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment