Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

difference between || and ?? in js

// The main difference is that nullish coalescing(??) operator will only
// give the result as the right operand only if the left operand is either null or undefined.

// Whereas the OR(||) operator will give the result as right operand 
// for all the falsy values of the left operand.

const a = 0;
// a || 10 --> Will result in 10, as || operator considers 0 as falsy value and resulting the right side operand
console.log(`a || 10 = ${a || 10}`);
// a ?? 10 --> Will result in 0, as ?? operator considers 0 as truthy value and resulting the left side operand
console.log(`a ?? 10 = ${a ?? 10}`);
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #difference #js
ADD COMMENT
Topic
Name
5+9 =