You can use the logical 'OR' operator in place of the Elvis operator:
For example displayname = user.name || "Anonymous" .
But Javascript currently doesn't have the other functionality. I'd recommend looking at CoffeeScript if you want an alternative syntax. It has some shorthand that is similar to what you are looking for.
For example The Existential Operator
zip = lottery.drawWinner?().address?.zipcode
Function shortcuts
()-> // equivalent to function(){}
Sexy function calling
func 'arg1','arg2' // equivalent to func('arg1','arg2')
There is also multiline comments and classes. Obviously you have to compile this to javascript or insert into the page as <script type='text/coffeescript>' but it adds a lot of functionality :) . Using <script type='text/coffeescript'> is really only intended for development and not production.
You can use the logical 'OR' operator in place of the Elvis operator:
For example displayname = user.name || "Anonymous" .
But Javascript currently doesn't have the other functionality. I'd recommend looking at CoffeeScript if you want an alternative syntax. It has some shorthand that is similar to what you are looking for.
For example The Existential Operator
zip = lottery.drawWinner?().address?.zipcode
Function shortcuts
()-> // equivalent to function(){}
Sexy function calling
func 'arg1','arg2' // equivalent to func('arg1','arg2')
There is also multiline comments and classes. Obviously you have to compile this to javascript or insert into the page as <script type='text/coffeescript>' but it adds a lot of functionality :) . Using <script type='text/coffeescript'> is really only intended for development and not production.
You can use the logical 'OR' operator in place of the Elvis operator:
For example displayname = user.name || "Anonymous" .
But Javascript currently doesn't have the other functionality. I'd recommend looking at CoffeeScript if you want an alternative syntax. It has some shorthand that is similar to what you are looking for.
For example The Existential Operator
zip = lottery.drawWinner?().address?.zipcode
Function shortcuts
()-> // equivalent to function(){}
Sexy function calling
func 'arg1','arg2' // equivalent to func('arg1','arg2')
There is also multiline comments and classes. Obviously you have to compile this to javascript or insert into the page as <script type='text/coffeescript>' but it adds a lot of functionality :) . Using <script type='text/coffeescript'> is really only intended for development and not production.
2020 Update
JavaScript now has equivalents for both the Elvis Operator and the Safe Navigation Operator.
Safe Property Access
The optional chaining operator (?.) is currently a stage 4 ECMAScript proposal. You can use it today with Babel.
// `undefined` if either `a` or `b` are `null`/`undefined`. `a.b.c` otherwise.
const myVariable = a?.b?.c;
The logical AND operator (&&) is the "old", more-verbose way to handle this scenario.
const myVariable = a && a.b && a.b.c;
Providing a Default
The nullish coalescing operator (??) is currently a stage 4 ECMAScript proposal. You can use it today with Babel. It allows you to set a default value if the left-hand side of the operator is a nullary value (null/undefined).
const myVariable = a?.b?.c ?? 'Some other value';
// Evaluates to 'Some other value'
const myVariable2 = null ?? 'Some other value';
// Evaluates to ''
const myVariable3 = '' ?? 'Some other value';
The logical OR operator (||) is an alternative solution with slightly different behavior. It allows you to set a default value if the left-hand side of the operator is falsy. Note that the result of myVariable3 below differs from myVariable3 above.
const myVariable = a?.b?.c || 'Some other value';
// Evaluates to 'Some other value'
const myVariable2 = null || 'Some other value';
// Evaluates to 'Some other value'
const myVariable3 = '' || 'Some other value';
Videos
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);
Many popular mainstream languages that focus at least somewhat on conciseness have a similar operator, so I'd say that it is fairly common.
First, some notes:
- For the purposes of this answer, it doesn't matter whether the syntax is
?:,??,||oror, especially since all are equally concise. - What is a "true value" for this operator differs from language to language, and may not be the same as what is considered a "true value" in other contexts (like in an
ifcondition).
Now, let's go though some languages and see what we find:
- C and C++: no Elvis in the standards, since concise syntax sugar does not seem to be focus for these languages
- Java: Elvis was explicitly rejected, seemingly also because syntax sugar is not a focus for Java.
- C#:
??is anull-checking Elvis - Python:
orworks as truthy-checking Elvis - JavaScript:
||works as an Elvis operator that checks for truthiness,??is an Elvis operator that only checks fornullandundefined - PHP:
?:is a truthy-checking Elvis,??is anull-checking Elvis - Ruby:
||works as a truthy-checking Elvis - Go: no Elvis, since Go prefers simplicity of language to conciseness of code
- Swift:
??is anil-checking Elvis - Rust: no Elvis, presumably because Rust does not have concepts similar to truthiness or
null
Some languages simply don't have (a relevant type of) nulls, or don't have the concept of non-Boolean thruthiness, making it a not applicable feature.
Often if you want to say, "x, if it exists, or y", you'd use the or operator ||.
Example:
const foo = bar || 3;
However, let's say you want to check that the value of foo exists. If foo is 0, then it would evaluate to false, and the above code doesn't work.
So instead, you can use the Nullish Coalescing Operator.
const foo = bar ?? 3;
This would evaluate to 3 if bar is undefined or null, and use the value of bar otherwise.
In typescript, this is useful for setting default values from a nullable object.
setFoo(foo: Foo|null) {
this.foo = foo ?? DEFAULT_FOO;
}