What Is a Nullish Value in JavaScript?

Find out what a nullish value means in JavaScript

In JavaScript, a nullish value is a value that is:

  • null, or;
  • undefined.

Nullsih values are always falsy.

For example, the nullish coalescing operator (??) returns its right-hand side operand when its left-hand side operand is a nullish value, and returns its left-hand side operand otherwise:

console.log(null ?? 'foo'); // 'foo'
console.log(undefined ?? 'foo'); // 'foo'
console.log(null ?? undefined ?? 'foo'); // 'foo'

console.log('bar' ?? 'foo'); // 'bar'

Another example could be about the optional chaining operator (?.), which stops a chain of connected objects and evaluates the entire expression to undefined if the left-hand side operand is a nullish value. For example:

console.log(null?.prop); // undefined
console.log(undefined?.prop); // undefined

Hope you found this post useful. It was published . Please show your love and support by sharing this post.