Skip to main content
0 reactions

How to Check if a Value Is undefined in JavaScript

Card titled Check for undefined in JavaScript, labelled JavaScript basics, with four ticked lines: author === undefined gives true, and title === undefined gives false.
How to Check if a Value Is undefined in JavaScript

undefined is the value JavaScript gives for something that has no value, such as an object property that doesn’t exist. To check for it, compare with === undefined.

Every command and output on this page is real, run with Node.js 26.13.1 on macOS.

Syntax

SyntaxJavaScript
value === undefined
  • value: what you want to check, such as book.author.
  • ===: strict equality. It’s true only when both sides are the same value and the same type.
  • undefined: the missing value itself, written without quotes.

See undefined (opens in a new tab) and Strict equality (===) (opens in a new tab) on MDN.

Prerequisites

The Example

A book object with a title and no author, in a file named undefined.js.

Step 1: Write the Code

undefined.jsJavaScript
const book = { title: 'Salt and Starlight' };

console.log(book.author === undefined);
console.log(book.title === undefined);

Step 2: Run It

TerminalBash
node undefined.js
OutputPlain text
true
false

book.author is undefined, because the object has no author, so the first check is true. The title has a value, so the second is false.

Common Mistakes

MistakeWhat happensDo this instead
Putting undefined in quotes: book.author === 'undefined'That compares with the text undefined, a string, so it printed false even though the author is missingWrite undefined without quotes

FAQ

Where else does undefined come up?

A function with no return gives back undefined, and so does find() on an array when nothing matches.

Is undefined the same as null?

No. undefined means no value was ever given. null is a value you set on purpose to mean empty. null === undefined is false.

Conclusion

Use value === undefined to find out whether a value is missing. Write undefined without quotes.

Additional Resources

JavaScript is a trademark of Oracle Corporation in the United States. Node.js is a trademark of the OpenJS Foundation.

0 comments