How to check if variable is undefined or null in JavaScript?

Today, we will show you the best ways to check for undefined or null variables or object in JavaScript. 

By using these solutions, you can improve your JavaScript applications and avoid potential errors.

In JavaScript programming, it’s really important to handle undefined and null values correctly to make sure your code works smoothly and reliably.



Let’s dive into the world of handling how to check null or undefined in JavaScript.

What is undefined or null means in JavaScript?

In JavaScript, when a variable is declared but not given a value, it is considered undefined.

On the other hand, null is a special value that represents the absence of a value or object.

You can assign null to a variable to indicate that it has no value.

For instance, if you declare a variable like:

 var i; 

And try to print its value using:

console.log(i);

Output:

undefined

It will display undefined because the variable has been declared, but not assigned any value.

However, if you assign null to the variable like:

 t = null;

And then print its value using:

console.log(t);

Output:

null

It will display null because the variable has been defined, but there’s a missing value.

In a simple understanding, undefined indicates that a variable has been declared but lacks a value or has not been assigned any value, while null is a value that denotes the absence of a value or object.

Different ways to check if variable is undefined or null in JavaScript

To check if a variable is undefined or null, you can use different methods in JavaScript.
Here are some common ways:

1. Use the equality operator (==)

You can use the equality operator (==) in JavaScript to check if a variable is undefined or null.

This operator compares values and converts them to the same type before making the comparison. Since both undefined and null are considered false values.

You can use the following code to check if a variable is undefined or null:

if (variable == null) {
  // variable is undefined or null
}

Here’s an example code:

let i;

if (i == null) {
  console.log("variable is undefined or null");
} else {
  console.log("variable is not undefined or null");
}

Output:

variable is undefined or null

2. Use the strict equality operator (===)

To check if a variable is undefined or null in JavaScript, you can use the strict equality operator (===).

Unlike the regular equality operator, it doesn’t convert the types of the values being compared.

It only returns true if the variable is of the same type and has the exact value of undefined or null.

Here’s the following code to check if a variable is undefined or null:

if (variable === undefined || variable === null) {
  // variable is undefined or null
}

Here’s an example code:

let t;

if (t === undefined || variable === null) {
  console.log("variable is undefined or null");
} else {
  console.log("variable is not undefined or null");
}

Output:

variable is undefined or null

3. Use the typeof operator

Another way to check if a variable is undefined, you can use the typeof operator in JavaScript.

It gives you the type of the variable as a string. If the variable is undefined, typeof will return “undefined”.

Here’s the following code to check if a variable is undefined or null:

if (typeof variable === "undefined") {
  // variable is undefined
}

Here’s an example code:

let t;

if (typeof t === "undefined") {
  console.log("variable is undefined");
} else if (t === null) {
  console.log("variable is null");
} else {
  console.log("variable is not undefined or null");
}

Output:

variable is undefined

Moreover, you can use the typeof operator to check if a variable is defined or undefined in JavaScript.

When you apply typeof to a variable, it will give you “undefined” if the variable is not initialized, and “null” if the variable intentionally has no value assigned to it.

Conclusion

To sum up, this article explores different approaches on how to check if a variable is undefined or null in JavaScript.

This article discussed the concepts of undefined and null in JavaScript, explaining that undefined represents a variable without a value or assignment, while null indicates the absence of a value or object.

We are hoping that this article provides you with enough information that helps you understand the JavaScript check undefined or null. 

You can also check out the following article:

Thank you for reading itsourcecoders 😊.

Quick step-by-step summary (click to expand)
  1. What is undefined or null means in JavaScript. Read the ‘What is undefined or null means in JavaScript?’ section for the details and code.
  2. Different ways to check if variable is undefined or null in JavaScript. Read the ‘Different ways to check if variable is undefined or null in JavaScript’ section for the details and code.
  3. Conclusion. Read the ‘Conclusion’ section for the details and code.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.

Caren Bautista


Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel
 · View all posts by Caren Bautista →

Leave a Comment