JavaScript type conversion: (true && 1) vs (true | | 1)

后端 未结 6 1786
清酒与你
清酒与你 2020-12-09 17:31

JavaScript is non-strictly typed language as Java,for example.

As we know, it converts value of result dependently upon context:

\"2\" + \"3\" r

6条回答
  •  無奈伤痛
    2020-12-09 18:17

    You can do other things too like:

    var myVar = Math.random() > 0.5;
    
    myVar && doFunc();
    

    which is the same as

    if(myVar) {
        doFunc();
    }
    

    The || basically means "If the first thing is false, go to second"

    The && basically means "If the first thing is true, go to the second"

    This is why you see things like this at the top of functions:

    function myFunction(options) {
        options = options || {};
    }
    

    Which means: If options is falsey, make it {}

提交回复
热议问题