let gender = user.male ? "male" : "female";
let displayName = user.name ?: "";
let displayName = user.name ?. "";
let displayName = user.name || "";
The above all operators are similarly doing the same process. What are the difference benefits and which one is best & worst?
The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.
The Safe Navigation Operator is also known as the "Elvis Operator". This operator is very useful to protect against null and undefined values in property paths. This operator allows us to navigate an object path in situations when we are not aware whether a path exists or not.
In certain computer programming languages, the Elvis operator, often written ?: , or or || , is a binary operator that returns its first operand if that operand evaluates to a true value, and otherwise evaluates and returns its second operand.
The Angular safe navigation operator, ? , guards against null and undefined values in property paths. Here, it protects against a view render failure if item is null .
update
With TypeScript 3.7 they've implemented Optional Chaining, which is like the safe navigation operator, but then better. Also the Nullish Coalescing
has found its way to the scene.
If you are using this version, the below answer is obsolete
Maybe I've missed a couple versions, but to my knowledge, TypeScript does not have an elvis operator or a safe navigation operator. The only extra thing they have is a non-null assertion operator !.
, but this is only for the compiler, and in the compiled js the !
will be removed. Angular however, does have the safe navigation operator inside their templates, but under the hood this will resolve into a logical or ||
. The benefit here is increased readability and smaller templates.
Besides that, TypeScript does have the ?:
notation, but this is used in interfaces or method parameters to indicate that the value is optional
Which leaves us with the ternary operator vs logical or. You would use the first one if there are 3 values. The question, the answer yes result, and the answer no result to said question.
And the latter when there are 2 values. Where the first one, if resolved to truthy will be the answer, and otherwise the second one, regardless of its value.
Benefit wise, I can't really say much. I would expect them to be equally fast, with a marginal difference. Perhaps readability is increased with the ternary option, which you obviously can always use instead of the logical or ||
, but personally I like to use the ||
, because it keeps the code compact.
TLDR;
Simplified if else, available everywhere
?:
Not available in typescript/javascript/angular and essentially the same as ||
?.
Only available in angular templating, used to prevent null pointers in object parameter navigation
||
If not left hand, then right hand. Even more simplified if else. Available in typescript/javascript/angular
let gender = user.male ? "male" : "female";
can Used in javascript(Typescript)
as well as in HTML
tag binding ,
Basically when you use this operator in javascript code it means if first statment is true than execute first otherwise execute second option
In angular2 Terms Ternary Operator
is known as Safe Navigation Operator (?.)
or you can use the term Elvis Operator (?: )
which is used at the time of fetching asynchronous data from the backend or some kind of databse.
alternate :- you can also use Elvis Operator (?: )
in angular2 template like this (we can say this is shorthand property)
let gender = user.gender || "";
Have a look here too
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With