Return result from Ternary in one line (JavaScript)

前端 未结 3 646
旧时难觅i
旧时难觅i 2021-01-29 16:09

In JavaScript, rather than having to assign the result to a variable, is it possible to return the result of a ternary in one line of code?

e.g. Instead of

3条回答
  •  日久生厌
    2021-01-29 16:25

    You can just return whatever a > b evaluates to.

     function isAGreaterThanB(){
         return a > b;
     }
    

    As a > b evaluates to either True or False, you can just return that value directly.

    Actually doing it how you typed is, is a really bad way to do it and is unneccessarily complicated for something as basic as this.

提交回复
热议问题