Allow zero value when evaluating short circuit

The short circuit rating determines whether the first value is false. If so, return the second value as shown below:

var x = y || z; // if y is falsey return z 

Is there a way to ignore null values โ€‹โ€‹as false when using short circuit evaluations without resorting to if / else statements or triple statements?

+7
javascript
source share
4 answers

You can wrap your number in a Number object and check it;

 var x = new Number(0) || console.log("never gets printed"); console.log(parseInt(x)); //or console.log(x.valueOf()); 
+2
source share

EDIT:

If z is a number, you can use a trick like this:

 var x1 = Number(y===0 && '0' || y || z) // or var x2 = (y===0 && '0' || y || z)-0 

 var z = -1; var y = 42; var x = y || z; var x1 = Number(y===0 && '0' || y || z) var x2 = (y===0 && '0' || y || z)-0 console.log('x:',x, ' x1:',x1, ' x2:',x2); var y = 0; var x = y || z; var x1 = Number(y===0 && '0' || y || z) var x2 = (y===0 && '0' || y || z)-0 console.log('x:',x, ' x1:',x1, ' x2:',x2); var y = null; var x = y || z; var x1 = Number(y===0 && '0' || y || z) var x2 = (y===0 && '0' || y || z)-0 console.log('x:',x, ' x1:',x1, ' x2:',x2); 

Original answer:

Maybe a simple way might work (3 equivalents)

 var x = (y === 0) ? 0 : (y || z); var x = (!y && y!==0) ? z : y; 

 var z = 'Was falsey'; var y = 42; var x = y || z; var x1 = (y === 0) ? 0 : (y || z); var x2 = (!y && y!==0) ? z : y; console.log('x:',x, 'x1:',x1, 'x2:',x2); var y = 0; var x = y || z; var x1 = (y === 0) ? 0 : (y || z); var x2 = (!y && y!==0) ? z : y; console.log('x:',x, 'x1:',x1, 'x2:',x2); var y = null; var x = y || z; var x1 = (y === 0) ? 0 : (y || z); var x2 = (!y && y!==0) ? z : y; console.log('x:',x, 'x1:',x1, 'x2:',x2); 
+1
source share

Edit: this uses the ternary operator, so if this is not what you are looking for, do not use this.

Of course, this is another easy way to do this:

 var x = y || y==0?0:z; 

if y true then x set to y

if y is false and y==0 , then x set to 0

if y is false and y!=0 , then x set to z ;

0
source share

First you can check if y non-zero and take a numeric value and get the default result z for y .

 x = +(y !== 0) && (y || z) 

How it works:

 expression y part result result comment ---------------------- --------- ----------- ------ ---------------------------- +(y !== 0) && (y || z) complete expression +(y !== 0) 0 0 0 result found, omit next part because of falsy value +(y !== 0) 1 1 check next part 1 && (y || z) yy take y, omit default +(y !== 0) undefined 1 check next part 1 && (y || z) zz take z as default 

 function x(y, z) { return +(y !== 0) && (y || z); } console.log(x(0, 42)); // 0 console.log(x(4, 42)); // 4 console.log(x(undefined, 42)); // 42 console.log(x(0, null)); // 0 console.log(x(4, null)); // 4 console.log(x(undefined, null)); // null console.log(x(0, 0)); // 0 console.log(x(4, 0)); // 4 console.log(x(undefined, 0)); // 0 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 
0
source share

All Articles