Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does ES6 support the Elvis operator? [duplicate]

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);
like image 778
1.21 gigawatts Avatar asked Oct 17 '18 06:10

1.21 gigawatts


2 Answers

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");
like image 161
Mild Fuzz Avatar answered Oct 22 '22 03:10

Mild Fuzz


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);

like image 3
Ronen Cypis Avatar answered Oct 22 '22 03:10

Ronen Cypis