SoFunction
Updated on 2025-03-10

Detailed explanation of the difference between null and undefined in JavaScript

1. Concept

null:

null is a primitive value that means "none" or "null value". It is usually used to indicate that the object or value that the variable should have does not exist. null
Can be assigned to a variable to show that the variable does not point to any object.

undefined:

undefined is a primitive value that means "undefined". JavaScript automatically sets it to undefined when a variable has been declared but has not been assigned.
It also means that the default return value when the object's attribute does not exist or the function does not have a return value.

2. Type

null:

When using the typeof operator to check the type, the type of null is reported as object. This is a historical issue.

Example:

let a = null;
(typeof a); // "object"

undefined:

The type of undefined is undefined.

Example:

let b;
(typeof b); // "undefined"

3. Use scenarios

Use of null:

  • null can be used when it is necessary to indicate "empty" or "invalid" state.
  • Usually used in functions to represent return values ​​without results.
  • Used to initialize a variable, indicating that the variable will be assigned a value in the future.

Example:

let user = null; // means that the user has not logged in yetfunction getUser() {
    return null; // User not found}

Use of undefined:

  • When a variable is declared but no assignment is set to undefined.
  • Accessing properties that do not exist in the object will return undefined.
  • Undefined will also be returned when the function does not return a value.

Example:

let name; // declared but not assigned(name); // undefined

let obj = {};
(); // undefined, the attribute does not exist
function noReturnValue() {}
(noReturnValue()); // undefined, the function does not return a value

4. Equal comparison

In equality comparisons, null and undefined are considered equal, but their strict comparisons are not equal:

(null == undefined); // true
(null === undefined); // false

The == operator performs type conversion, so null and undefined are considered equal.
The === operator does not perform type conversion, so the two types are different, and returns false.

5. Specific case analysis

5.1 Common Mistakes in Using null and undefined

Some developers may mix null and undefined, but they should choose the appropriate value according to the actual situation. For example, null is applied to a missing value, while undefined means that the variable has not been assigned yet.

5.2 JSON and null

In JSON, null is a valid value, and undefined is not serialized in JSON.

let jsonObject = {
    name: "Alice",
    age: null, // Legal    height: undefined // will not be included in JSON string};

((jsonObject)); // '{"name":"Alice","age":null}'

6. Choice in practical applications

Select null:

When you want to explicitly indicate that the value of a variable is empty, for example in database operations, null is usually used to represent missing data.
For example, when a form is submitted, if the user does not enter a field, it can be set to null.

Select undefined:

Use undefined when you need to check if the variable is initialized or if the property exists.
For optional parameters, unpassed parameters will default to undefined.

7. Summary

null and undefined are special values ​​in JavaScript that represent missing or non-existent values.
null is a intentional assignment, indicating "no value"; while undefined usually means "missing value" or "uninitialized".
Understanding their differences is essential for writing clear and easy-to-maintain code.