Typically, we write
if else
conditions like this:
We can also write that
code as shown below:
As we can see, this style of
writing makes the code much simpler.
In this case, the question mark
is acting like a ternary operator.
We write the condition
before the question mark, ?
.
If the condition is true,
then the item before
:
is returned.
And if the condition is false,
then the item after :
is returned.
Let's see one more example.