Site icon JD Bots

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

Syntax

value === undefined

See undefined and Strict equality (===) 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.js

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

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

Step 2: Run It

Terminal

node undefined.js

Output

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.

Exit mobile version