undefined or null in JavaScript: How to Check a Variable in 2025

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

"Variable is not defined": why does this question torment everyone?

\n\n

If you've ever written in JavaScript, you've definitely encountered this situation: you run the code, and in the console there's a red message Uncaught TypeError: Cannot read properties of undefined. Or even worse — null is not an object. Sound familiar? This is one of the most common errors that plagues developers of all levels.

\n\n

The question "How can I determine if a variable is 'undefined' or 'null'?" on Stack Overflow has gathered thousands of views and hundreds of answers. And for good reason: in JavaScript, these two values are similar but behave differently. In 2025, when code becomes more complex and projects become larger, the ability to correctly check for undefined and null transforms from "good to know" to "must know".

\n\n

In this article, we'll break down all the checking methods, from simple to advanced, and show which methods are worth using in modern development. Let's go!

\n\n

undefined vs null: what's the difference?

\n\n

Before checking, let's understand what we're dealing with. In JavaScript, undefined and null are two different primitive values, each with its own type.

\n\n

undefined

\n

undefined is a value that is automatically assigned to a variable if you declared it but didn't assign a value. Also, a function returns undefined if it doesn't have an explicit return.

\n\n
let myVar;\nconsole.log(myVar); // undefined\n\nfunction doNothing() {}\nconsole.log(doNothing()); // undefined\n\nconsole.log(someNonExistentVariable); // ReferenceError, but if accessing an object property:\nconst obj = {};\nconsole.log(obj.prop); // undefined\n
\n\n

null

\n

null is a value that a developer assigns intentionally. It means "empty", "nothing", "value is absent". null is an object from the typeof perspective (yes, this is a historical bug, but it has already become a feature).

\n\n
let myVar = null;\nconsole.log(myVar); // null\nconsole.log(typeof myVar); // "object" — yes, that's right!\n
\n\n

The main difference: undefined is "not yet assigned", while null is "intentionally empty". But with loose comparison, they are equal:

\n\n
console.log(null == undefined); // true\nconsole.log(null === undefined); // false\n
\n\n

It is precisely because of this loose equality that confusion arises. Therefore, the best practices of 2025 recommend using strict comparison (===).

\n\n

Top 5 ways to check for undefined and null

\n\n

Let's break down all the popular methods, from the simplest to the most modern. We'll evaluate each by three criteria: readability, reliability, and performance.

\n\n

1. Strict comparison (===) — the gold standard

\n

The most direct and understandable way is to compare the variable directly with undefined or null using ===.

\n\n
let value = null;\n\n// Check for null\nif (value === null) {\n    console.log('This is null');\n}\n\n// Check for undefined\nif (value === undefined) {\n    console.log('This is undefined');\n}\n\n// Check for null OR undefined (separately)\nif (value === null || value === undefined) {\n    console.log('Value is absent');\n}\n
\n\n

Why is this the best practice?

\n
    \n
  • Maximally readable — any developer will understand what you are checking.
  • \n
  • No unexpected type coercions.
  • \n
  • Works in all versions of JavaScript, including the oldest ones.
  • \n
\n\n

Drawback: If you need to check both values at once, you have to write two conditions. But this is solved with the || operator, as in the example above.

\n\n

2. Nullish coalescing operator (??) — a new feature that has caught on

\n

Starting from ES2020, JavaScript introduced the ?? operator (nu

Blogs

Book Recommendations