javascript

Null

Learn about the 'null' data type in JavaScript. Understand what 'null' means, how it differs from 'undefined', and its practical uses for clearing values and indicating absence.

9 min read 8 sections Tutorial
Share

This page will teach you all about the `null` data type in JavaScript. You will learn what `null` is, why it is important, and how to use it correctly in your code. Understanding `null` helps you write cleaner and more predictable JavaScript programs.

Null

Null

This page will teach you all about the null data type in JavaScript. You will learn what null is, why it is important, and how to use it correctly in your code. Understanding null helps you write cleaner and more predictable JavaScript programs.

What is Null?

null is a special value in JavaScript that means "no value" or "empty." It is often used to show that a variable should intentionally have no object value.

Empty Value

null means a variable intentionally holds no value or object.

Primitive Type

null is one of JavaScript's seven primitive data types.

Falsy Value

In a boolean context, null behaves like false.

Specific Use

Developers use null to explicitly clear a variable's value.

What is Null?

Getting Started

What is Null?

null is a special keyword in JavaScript. It means that something has no value. It is like an empty box that you know is empty on purpose. We use null when we want to say that a variable should not point to any object or value right now.

javascript
1// Declare a variable called 'user' and give it a value
2let user = "Alice";
3console.log(user); // This will show "Alice"
4
5// Now, we want to clear the user's information
6// We set 'user' to null to show it has no value
7user = null;
8console.log(user); // This will show null

Don't Confuse Null with Zero or Empty String

null is not the same as 0 (the number zero) or "" (an empty string). These values are actual values. null specifically means the absence of any value. For example, null is not equal to 0.

Null vs. Undefined

Null vs. Undefined

JavaScript has another special value called undefined. It also means "no value." But null and undefined are different. null means you intentionally set a variable to have no value. undefined means a variable has been declared but has not yet been given a value. It can also mean a property that doesn't exist on an object.

javascript
1// A variable that has been declared but not assigned a value is 'undefined'
2let userName;
3console.log(userName); // This will show 'undefined'
4
5// A variable that is explicitly set to have no value is 'null'
6let currentSelection = null;
7console.log(currentSelection); // This will show 'null'
8
9// If you try to access a property that doesn't exist on an object, it's also 'undefined'
10let person = { name: "Bob" };
11console.log(person.age); // This will show 'undefined'
✗ BadLoose Equality (==)
// Using double equals (==) checks for value, ignoring type.
// null and undefined are considered loosely equal.
console.log(null == undefined); // true
// But they are not equal to 0 or an empty string.
console.log(null == 0); // false
console.log(null == ''); // false
✓ GoodStrict Equality (===)
// Using triple equals (===) checks for both value AND type.
// null and undefined are different types, so they are not strictly equal.
console.log(null === undefined); // false
// The type of null is 'object' (a JavaScript quirk).
// The type of undefined is 'undefined'.
console.log(typeof null); // 'object'
console.log(typeof undefined); // 'undefined'

Checking for Null Values

Checking for Null Values

When you write code, you often need to check if a variable currently holds a null value. This helps you make sure your program acts correctly. There are a few ways to check for null. The most common ways use the equality operators.

Using Loose Equality (==)
// Loose equality (==) checks if a value is null OR undefined.
// This can be useful if you treat both as 'no value'.
let myValue = null;
if (myValue == null) {
console.log("myValue is null or undefined.");
}
myValue = undefined;
if (myValue == null) {
console.log("myValue is null or undefined.");
}
VS
Using Strict Equality (===)
// Strict equality (===) checks if a value is EXACTLY null.
// This is generally safer because it does not also match undefined.
let myValue = null;
if (myValue === null) {
console.log("myValue is strictly null.");
}
myValue = undefined;
if (myValue === null) {
// This block will NOT run because undefined !== null
console.log("myValue is strictly null.");
} else {
console.log("myValue is not strictly null.");
}

Prefer Strict Equality

It is generally a good practice to use === null when you specifically want to check for null. This makes your code clearer. It also prevents unexpected matches with undefined or other falsy values. Use == null only if you truly mean to catch both null and undefined.

Practical Uses of Null

Practical Uses of Null

null is not just a theoretical concept. It is used in many real-world programming situations. You will often see null used to reset values, clear user input, or indicate that a function could not find something it was looking for. It helps manage the state of your application.

1

Clear a User's Selection

Imagine a user selects an item. When they deselect it, you can set the selectedItem variable to null.

javascript
1// User selects an item
2let selectedItem = "Laptop";
3console.log("Selected:", selectedItem);
4
5// User deselects the item
6selectedItem = null;
7console.log("Selected:", selectedItem); // Shows null
2

Reset a Form Field

After a user submits a form, you might want to clear the input fields. Setting their values to null is one way to do this.

javascript
1// Imagine a form input's value
2let usernameInput = "chillucoder";
3console.log("Input value before reset:", usernameInput);
4
5// Reset the input after submission
6usernameInput = null;
7console.log("Input value after reset:", usernameInput); // Shows null
3

Indicate No Result Found

A function might return null if it tries to find something but cannot. This tells your code that nothing was found.

javascript
1// A function that tries to find a user by ID
2function getUserById(id) {
3 if (id === 101) {
4 return { id: 101, name: "Alice" };
5 } else {
6 // If no user is found, return null
7 return null;
8 }
9}
10
11let foundUser = getUserById(101);
12console.log(foundUser); // Shows { id: 101, name: 'Alice' }
13
14let noUser = getUserById(999);
15console.log(noUser); // Shows null
16
17if (noUser === null) {
18 console.log("User not found!");
19}

Beware of Null Reference Errors

If a variable is null, you cannot try to access properties or methods on it. Doing so will cause a TypeError. For example, if user is null, user.name will cause an error. Always check for null before trying to use an object.

Null in JavaScript

Null in JavaScript

PropertyValue/BehaviorExplanation
Type (typeof)`"object"`This is a known quirk in JavaScript. `null` is a primitive, but `typeof null` returns `"object"`.
Falsy Value`true`When used in a boolean context (like an `if` statement), `null` acts like `false`.
Equality (`==`)Equals `undefined``null == undefined` is `true`. `null` is only loosely equal to `undefined`.
Equality (`===`)Only equals `null``null === null` is `true`. `null === undefined` is `false` because their types are different.

Test Your Knowledge

Test Your Knowledge

Test Your Knowledge

Quick Check

What does null represent in JavaScript?

Quick Check

Which of the following statements is true?

Quick Check

What is the result of console.log(typeof null);?

Quick Check

When should you generally use null?

Quick Reference

Quick Reference

Quick Reference

Pro Tips
  • 1Definitionnull means the intentional absence of any object value.
  • 2Primitive Type — It is one of JavaScript's seven primitive data types.
  • 3typeof quirktypeof null returns "object", a historical bug.
  • 4Falsy Valuenull behaves like false in boolean contexts.
  • 5== Equalitynull == undefined evaluates to true (loose equality).
  • 6=== Equalitynull === undefined evaluates to false (strict equality, different types).
  • 7Explicit Assignment — Use null when you want to explicitly clear a variable's value or reset a state.
  • 8Error Prevention — Always check if a variable is null before trying to access its properties or methods to avoid TypeError.
  • 9Not Zero/Empty Stringnull is distinct from 0 and ""; it specifically means 'no value', not an 'empty value' of a specific type.
  • 10Best Practice — Prefer === null for precise checks to differentiate from undefined.

Data Types

Explore other JavaScript data types like numbers, strings, and booleans.

Undefined

Learn more about undefined and its key differences from null.

Truthiness and Falsiness

Understand how values like null behave in conditional statements.

Equality Operators

Dive deeper into == (loose) vs === (strict) equality in JavaScript.

You've Mastered Null!

Congratulations! You now understand what null is, how it differs from undefined, and how to use it effectively in your JavaScript programs. This knowledge is key to writing robust and clear code.

Try it in the Javascript Compiler

Run and experiment with Javascript code right in your browser — no setup needed.

Continue Learning