W3Schools JavaScript If Null: Master Conditional Checks and Undefined Handling

Robert Jul 13, 2026

Ever wondered how to check if a JavaScript variable is null? Well, you've come to the right place. In JavaScript, the equality and inequality operators (like === and !==) can throw a TypeError if used to compare a variable to null directly. This is where conditional statements like if else come into the picture. Let's dive right in.

JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)

Before we proceed, it's crucial to understand that null is an assignment value that means 'no value'. Let's see how we can check if a variable is null using JavaScript's if else statement.

Javascript Fundamental
Javascript Fundamental

Checking for Null with if else

In JavaScript, you can use an if else statement to check if a variable is null. Here's a simple example:

Javascript window.confirm() method | Javascript tutorial
Javascript window.confirm() method | Javascript tutorial

```javascript let x = null; if (x === null) { console.log("Variable x is null"); } ```

Using Strict Equality (===)

JavaScript Projects That Will Make You Better Fast 😳
JavaScript Projects That Will Make You Better Fast 😳

The strict equality operator (===) checks both value and type. So, when you compare a variable to null, it ensures that x is null and not any other type or value that might be equal to null.

```javascript let y = 0; if (y === null) { console.log("This will never be logged"); } ```

Using Logical Operators

JavaScript Fundamental
JavaScript Fundamental

If you want your code to be more readable, you can use logical operators to check if a variable is null. For instance, the ! operator can negate the truthiness of a variable:

```javascript let z = null; if (!z) { console.log("Variable z is null"); } ```

Checking for Null with typeof Operator

Javascript Read Excel
Javascript Read Excel

The typeof operator can help you in many cases. It returns a string indicating the type of the unevaluated operand. So, you can check if a variable is null this way:

```javascript let a = null; if (typeof a === "undefined") { console.log("Variable a is null"); } ```

5 Vanilla JavaScript Projects for Beginners
5 Vanilla JavaScript Projects for Beginners
JavaScript | Chapter 2 📖| Part 1 ✨
JavaScript | Chapter 2 📖| Part 1 ✨
JavaScript calculator projet
JavaScript calculator projet
Best Way to Learn JavaScript in 2024 (Free and Paid Tutorials)
Best Way to Learn JavaScript in 2024 (Free and Paid Tutorials)
add active class in javascript
add active class in javascript
a poster with different types of projects and instructions for students to use in the classroom
a poster with different types of projects and instructions for students to use in the classroom
HTML vs CSS vs JavaScript: What Each One Actually Does (Beginner's Visual Guide)
HTML vs CSS vs JavaScript: What Each One Actually Does (Beginner's Visual Guide)
Examination Apps in JavaScript with Source Code
Examination Apps in JavaScript with Source Code
Permutations & Combinations in Javascript
Permutations & Combinations in Javascript
JavaScript study guide for fast learning
JavaScript study guide for fast learning
an image of a web page with different types of text
an image of a web page with different types of text
different types of logos and their meanings
different types of logos and their meanings
Dynamic Class Toggle in DOM
Dynamic Class Toggle in DOM
sunset evening using html css javascript
sunset evening using html css javascript
Vanilla Javascript AJAX – A Beginner’s Tutorial
Vanilla Javascript AJAX – A Beginner’s Tutorial
8 Essential JavaScript Array Methods Visual Guide
8 Essential JavaScript Array Methods Visual Guide
Variables in JavaScript Explained Simply
Variables in JavaScript Explained Simply
Javascript setTimeout() method
Javascript setTimeout() method
CodeGuppy: The JavaScript Platform for Kids
CodeGuppy: The JavaScript Platform for Kids

Checking for Null with Global Object

JavaScript's global object has a property called ‘undefined’. So, you can use this to check if a variable is null:

```javascript let b = null; if (this['undefined'] === b) { console.log("Variable b is null"); } ```

Now that you've learned how to check if a JavaScript variable is null, why not try implementing these methods in your own projects? The key is to understand that null is an assignment value, not a data type, and treat it accordingly in your comparisons. Happy coding!