liveBook · Manning

Missing value

As explained in JavaScript in Depth

A special JavaScript value representing an intentional emptiness or explicit absence of an object value.

`null` represents deliberate emptiness: someone checked and chose to say there is no value. `undefined` commonly means a value is unset, a property is missing, or a function has no return value. These are conventions, not formal rules. section 4.1

Common conventions distinguish the two forms of absence:

  • Use `undefined` for uninitialized variables, missing arguments or properties, and work not yet done. section 4.1
  • Use `null` for intentionally empty values, a checked query with no result, or clearing a previously set value. section 4.1
function processValue(val) {
  if (val === undefined) {  #A
    return 'not set';  #A
  }  #A
 
  if (val == null) {  #B
    return 'no value';  #B
  }  #B
 
  return `processing ${val}`;  #C
}

A strict check can single out `undefined`; `val == null` is a deliberate loose-equality check that matches both `null` and `undefined`. section 4.1

Assigning `null` removes one reference path to an object; it does not guarantee reclamation if another path still reaches the object. section 9.1.1

How JavaScript in Depth covers it

How deep it goes
Defines null by contrast with undefined, gives usage conventions and equality behavior, and briefly notes V8 representation.
Examples
Uses empty database results, cleared values, null-versus-undefined checks, and nulling an object reference.
What you can do after
Evaluate absence checks by distinguishing deliberate emptiness from an unset value and accounting for `null == undefined`.
How it connects
Connects null to undefined, loose equality, truthiness, coercion, and garbage-collection reachability.

Also explained in

Compare how 39 books explain missing value →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.