Null and Undefined

The Special Keywords Null and Undefined

Null

Null is a special keyword in JavaScript that represents the absence of a value. Think of it as a placeholder that says, “there’s supposed to be something here, but right now there isn’t.” Technically, if you check the type of null, JavaScript will tell you it’s an object, which is a bit strange. You can imagine null as a unique kind of object that specifically indicates “no object” or “nothing here.”

Undefined

On the other hand, JavaScript has another way to represent the absence of a value, and that’s with undefined. Unlike null, undefined is a bit more like a marker that JavaScript uses when something is missing or hasn’t been set yet. For example, if you create a variable but don’t give it a value, that variable is undefined. Similarly, if you try to access a property of an object or an element in an array that doesn’t exist, you’ll get undefined. It’s also the default return value for functions that don’t explicitly return anything and for function parameters when you don’t pass an argument.

The differences

A key difference between null and undefined is that undefined is a predefined global constant, while null is a language keyword. If you ask JavaScript what type undefined is, it will tell you “undefined,” which shows that undefined is the sole member of its own type.

Although null and undefined are different, they often get used in similar ways to indicate that something is missing or not set.

Understanding the nuances between null and undefined takes time, but as you continue to write and debug JavaScript code, these concepts will start to make more sense.

Example of null Explicitly Assigning null to a Variable:

let person = null;
console.log(person); // Output: null

Example of undefined Variable Declared but Not Initialized:

let age;
console.log(age); // Output: undefined

Study Style Notes

Null

  • Definition: null is a special keyword in JavaScript representing the absence of a value.
  • Purpose: Acts as a placeholder indicating “there’s supposed to be something here, but right now there isn’t.”
  • Type: JavaScript identifies null as an object (though it's a unique kind of object indicating "no object" or "nothing here").

Undefined

  • Definition: undefined represents the absence of a value, typically when something is missing or hasn’t been initialized.
  • Usage:
    • A variable declared but not assigned a value is undefined.
    • Accessing a non-existent object property or array element returns undefined.
    • Functions that don’t explicitly return a value return undefined.
    • Function parameters that aren’t passed an argument are undefined.
  • Type: undefined is a predefined global constant, and its type is undefined, making it the sole member of its type.

Key Differences

  • null vs undefined:
    • null is a language keyword, while undefined is a predefined global constant.
    • null indicates an intentional absence of any object value, while undefined typically signals an unintentional absence (e.g., uninitialized variables).

Common Uses

  • null and undefined: Both null and undefined are used to represent missing or unset values, though they serve slightly different purposes.