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
This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.