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
value === undefined
value: what you want to check, such asbook.author.===: strict equality. It’strueonly 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
- Node.js, to run the example. Install it from the Node.js download page (opens in a new tab).
The Example
A book object with a title and no author, in a file named undefined.js.
Step 1: Write the Code
const book = { title: 'Salt and Starlight' };
console.log(book.author === undefined);
console.log(book.title === undefined);
Step 2: Run It
node undefined.js
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
| Mistake | What happens | Do 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 missing | Write 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
- undefined (opens in a new tab) (MDN)
- Strict equality (===) (opens in a new tab) (MDN)
- Run Node.js scripts from the command line (opens in a new tab) (Node.js docs)
JavaScript is a trademark of Oracle Corporation in the United States. Node.js is a trademark of the OpenJS Foundation.
0 comments