Return result from Ternary in one line (JavaScript)

前端 未结 3 675
旧时难觅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:40

    Yes it is possible, you could for example say this:

    function getBiggerNumber(a, b){
        return a > b ? a : b
    }
    

    this function returns a if a is bigger than b and b if b is bigger than a.
    Just for completeness: It would also return b if a and b would be equal

提交回复
热议问题