Does Javascript / ES6 support the Elvis operator?
Example:
var x = (y==true) ?: 10;
Example 2:
var debug = true;
debug ?: console.log("Value of x:" + x);
Does Javascript / ES6 support the Elvis operator?
Example:
var x = (y==true) ?: 10;
Example 2:
var debug = true;
debug ?: console.log("Value of x:" + x);
No, but you can just use || or &&, seems to perform same function.
var debug = true;
debug && console.log("debug mode on ");
debug || console.log("debug mode off");
The short answer to your answer is "No". There is no Elvis operator in javascript. But you can achieve the same behavior in a few different short ways like so:
Using plain ternary operator:
var x = y ? 10 : null;
Or using a simple 'if' for just a single output:
if (debug) console.log("Value of x:", x);
||
-var x = y || 10
for example ordebug || console.log()
if (y === true) 10;
Note that this is not the most popular way of writing if statements cause of lack of readability.||
is the Elvis operator.||
only evaluates if the condition is false. The Elvis operator evaluates if the condition is true. So&&
is closer to the Elvis operator.||
does:1 || 2 // 1
vs1 && 2 // 2
.