Differences between Null and Undefined in JavaScript

Differences between Null and Undefined in JavaScript

In JavaScript, both null and undefined are special values that indicate the absence or lack of a meaningful value.

However, they are used in slightly different contexts and have some differences as well. Let’s see –

Undefined in JavaScript

  • When a variable is declared but not assigned a value, it is automatically (implicitly) initialized with undefined.

    let name; // Variable is Declared But Not Assigned a Value
    console.log(name); // Output: undefined
    
    name = "John"; // Assigning a Value (John) to the name Variable
    console.log(name); // Output: John
  • If a function does not have a return statement, it will return undefined by default.

    function sayHi() {
        console.log("HI");
    }
    
    const data = sayHi();
    console.log(data); // Output: undefined
  • When you access a property that doesn’t exist in an object, JavaScript will return undefined to indicate that the property is not present or hasn’t been assigned a value.

    const user = {
        name: "John",
        age: 23,
        email: "dd",
    };
    
    console.log(user.email); // Output: undefined
    
    /**
     * Use `in` operator or hasOwnProperty() to check
     * whether property is exist
     */
    if ("email" in user) {
        console.log(user.email);
    }
  • An explicit assignment of undefined is possible but generally not recommended, as it can be misleading and it is better to rely on JavaScript’s default behavior for uninitialized variables.

    let user1 = undefined; // ❌
    let user2 = null; // ✔️ use `null` instead.

Null in JavaScript

  • null is a value that needs to be assigned explicitly to indicate the intentional absence of a value or emptiness.

    let user = null; // Empty variable
  • JavaScript implicitly converts null to 0 when performing arithmetic operations.

    console.log(null + 2); // 2 because 0 + 2 = 2
    console.log(null * 5); // 0 because 0 * 5 = 0

Differences between Null and Undefined

undefinednull
Undefined means a variable has been declared but no value has been assigned yet.You can put null in a variable to say there’s nothing here.
undefined itself is a type as well as a value.null is a primitive type of value in JS.
When performing arithmetic operations it returns NaN – which means ‘not a number’.JS converts null to 0 when performing arithmetic operations.

Why is null an object?

null is a primitive type of value, but when you check its type using typeof you will get object.

console.log(typeof null); // Output: object

Many people consider this behavior a bug. Changing or fixing this bug may break a lot of existing code that relied on this behavior, so it has not been changed yet. But this is a theory.

There is another popular theory behind this. In the early versions of JavaScript, the first 3 bits of a value stored in 32 bits represented the data type and the remaining bits represented the value.

At that time the first 3 bits of all object type values started with 000. Null usually means empty, so it had all 0’s stored in 32 bits. JavaScript sees the starting 3 zeros in null, thinks it’s an object, and goes with it.

why null is object type

This mistake has persisted over time due to the need to maintain backward compatibility with existing code.