Logical AND (&&) and OR (||) operators

后端 未结 1 660
感情败类
感情败类 2021-01-15 17:50

Logical AND (&&) and OR (||) operators --- who knew they could trick us like this :)

Their definition, for JS (according to this ex

相关标签:
1条回答
  • 2021-01-15 18:06

    It's just how they work. It's not a bug:

    Returns expr1 if it can be converted to false; otherwise, returns expr2

    This means you can use "default values", like this:

    function someFunc(passedParameter){
        var newValue = passedParameter || 1337
    }
    

    Or run functions when conditions are met:

    var myBool = true;
    myBool && someFunc(); // someFunc will only be evaluated if `myBool` is truthy
    

    More info on truthy / falsy

    0 讨论(0)
提交回复
热议问题