Wednesday, 18 September 2013

Is it possible to do an inline "else if"?

Is it possible to do an inline "else if"?

This is really unnecessary, but I'm curious... can you do an inline "else
if" much like you would do a standard inline if/then/else? A few minutes
of googling did not return an answer.
Example:
if (n == 1) {
alert("The variable is 1!")
} else {
alert("The variable is not 1!")
}
is the same as:
n == 1 ? alert("The variable is 1!") : alert("The variable is not 1!")
But is there a way to do this using similar syntax?
if (n == 1) {
alert("The variable is 1!")
} else if (n == 2) {
alert("The variable is 2!")
} else {
alert("The variable is not 1 or 2!")
}
I tried the following, but it did not seem to work:
n == 1 ? alert("The variable is 1!") : (n == 2 ? alert("The variable is
2!") : alert("The variable is not 1 or 2!"))

No comments:

Post a Comment