---
title: "How do you check if a variable is undefined in JavaScript?"
description: "Compare with === undefined for declared variables, use typeof for names that may not exist, and use == null to cover null as well."
url: "https://bugsee.com/answers/javascript/check-if-a-variable-is-undefined/"
---

# How do you check if a variable is undefined in JavaScript?

For a declared variable use `value === undefined`; for a variable that might not exist at all use `typeof value === "undefined"`.

```js
let a;
a === undefined;                 // true

typeof notDeclaredAnywhere === "undefined"; // true, no ReferenceError

value == null;                   // true for both null and undefined
value ?? "fallback";             // use a default for null or undefined
```

Reading an undeclared name throws a `ReferenceError`, but `typeof` is the one operator that is allowed to look at it safely. Reach for `== null` or the `??` operator when null and undefined should be treated the same.

## Related questions

- [How do you check if a key exists in a JavaScript object?](https://bugsee.com/answers/javascript/check-if-a-key-exists-in-an-object/)
- [What is the difference between let and var in JavaScript?](https://bugsee.com/answers/javascript/difference-between-let-and-var/)
