简体   繁体   中英

How to write this in shorthand javascript

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 ?: .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM