I know how to write one line in a shorthand javascript if else statement. I am not sure how to write two. I keep getting an error with my syntax. This is what I am trying to convert into shorthand.
if (node === newValue) {
console.log('did find case')
return true
} else {
console.log('didn\'t find case')
}
I am trying to convert the above code into this but I am receiving an error
node === newValue
? console.log('did find case')
true
: console.log('didn\'t find case')
?:
and if
are not equivalent, and in particular one shouldn't think of ?:
as a "shorthand if
". if
works on statements and statement blocks; ?:
works on expressions. While all expressions are statements and many statements are also expressions, return
is not an expression: there is no way to use it inside ?:
.