In JavaScript, null means a value is empty on purpose. To check for it, compare the value with === null. To check that a value isn’t null, use !== null.
Every command and output on this page is real, run with Node.js 26.13.1 on macOS.
Syntax
value === null
value !== null
value: the variable or expression to check.===: strict equality. It givestrueonly when both sides are exactly the same value.!==: strict inequality, the opposite. It givestruewhen the two sides differ.null: written in lowercase, without quotes.
See null (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 contact with no phone number yet, so phone is null, and an email address. The file is contact.js.
Step 1: Write the Code
const phone = null;
const email = "sam@example.com";
console.log(phone === null);
console.log(email === null);
console.log(email !== null);
Each console.log prints the result of one check.
Step 2: Run It
node contact.js
true
false
true
phone === null is true. The email address is a string, so email === null is false and email !== null is true.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Checking with typeof value === "null" | It’s never true: typeof null gives "object" | Compare with === null |
Using == null when you mean only null | null == undefined is true, so undefined passes the check too | Use === null |
Writing "null" in quotes | That’s a string of four letters, not null | Write null without quotes |
FAQ
What’s the difference between null and undefined?
undefined means a value was never set, such as a property an object doesn’t have. null is set to empty on purpose. Strict equality tells them apart: null === undefined is false.
Conclusion
Compare with === null to find a value that’s empty on purpose, and with !== null to make sure a value holds something.
Additional Resources
- null (opens in a new tab) (MDN)
- Strict equality (===) (opens in a new tab) and Strict inequality (!==) (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