Can someone please explain to me in simple words what is the ?:
(conditional, "ternary") operator and how to use it?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
It is called the ternary operator
Hey mate just remember js works by evaluating to either true or false, right?
let's take a ternary operator :
First, js checks whether questionAnswered is
true
orfalse
.if
true
(?
) you will get "Awesome!"else (
:
) you will get "damn";Hope this helps friend :)
It's called the
ternary operator
. For some more info, here's another question I answered regarding this:How to write an IF else statement without 'else'
This is probably not exactly the most elegant way to do this. But for someone who is not familiar with ternary operators, this could prove useful. My personal preference is to do 1-liner fallbacks instead of condition-blocks.
Ternary Operator
is equivalent to
except, of course, it's shorter.