Skip to main content
0 reactions

How to Check if a Value Is null in JavaScript

A card titled Check for null, labelled JavaScript Basics, showing that phone === null and email !== null both give true.
Check for null: Find out whether a JavaScript value is empty on purpose.

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

SyntaxJavaScript
value === null
value !== null
  • value: the variable or expression to check.
  • ===: strict equality. It gives true only when both sides are exactly the same value.
  • !==: strict inequality, the opposite. It gives true when 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

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

contact.jsJavaScript
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

TerminalBash
node contact.js
OutputPlain text
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

MistakeWhat happensDo this instead
Checking with typeof value === "null"It’s never true: typeof null gives "object"Compare with === null
Using == null when you mean only nullnull == undefined is true, so undefined passes the check tooUse === null
Writing "null" in quotesThat’s a string of four letters, not nullWrite 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

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

0 comments