??) and the OR operator (
||) are both logical operators. However, they differ in the following ways:
- The OR operator returns the value on the right if the expression on the left is falsy, while the nullish coalescing operator returns the value on the right if expression on the left is a nullish value (i.e. null or undefined);
- The OR operator is supported in all old and modern browsers; the nullish coalescing operator (introduced in ES11) is well-supported in modern browsers.
'' || 'foo'; // 'foo' 0 || 'foo'; // 'foo' NaN || 'foo'; // 'foo' null || 'foo'; // 'foo' undefined || 'foo'; // 'foo'
// ES11+ '' ?? 'foo'; // '' 0 ?? 'foo'; // 0 NaN ?? 'foo'; // NaN null ?? 'foo'; // 'foo' undefined ?? 'foo'; // 'foo'
Hope you found this post useful. It was published . Please show your love and support by sharing this post.