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

后端 未结 6 1804
清酒与你
清酒与你 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:12

    Check Douglas Crockford's site, it says:

    The && operator is commonly called logical and. It can also be called guard. If the first operand is false, null, undefined, "" (the empty string), or the number 0 then it returns the first operand. Otherwise, it returns the second operand. This provides a convenient way to write a null-check:

    var value = p && p.name; /* The name value will only be retrieved from
    p if p has a value, avoiding an error. */
    

    The || operator is commonly called logical or. It can also be called default. If the first operand is false, null, undefined, "" (the empty string), or the number 0, then it returns the second operand. Otherwise, it returns the first operand. This provides a convenient way to specify default values:

    value = v || 10; /* Use the value of v, but if v doesn't have a value,
    use 10 instead. */
    

提交回复
热议问题