What's the difference between ( | ) and ( || )?

前端 未结 7 1203
故里飘歌
故里飘歌 2020-12-01 14:08

What\'s the difference between | and || in Javascript?

Furthermore, what\'s the difference between & and &&<

相关标签:
7条回答
  • 2020-12-01 14:47

    Another difference is that || uses shortcut evaluation. That is, it only evaluates the right side if the left side is false (or gets converted to false in a boolean context, e.g. 0, "", null, etc.). Similarly, && only evaluates the right side if the left side is true (or non-zero, non-empty string, an object, etc.). | and & always evaluate both sides because the result depends on the exact bits in each value.

    The reasoning is that for ||, if either side is true, the whole expression is true, so there's no need to evaluate any further. && is the same but reversed.

    The exact logic for || is that if the left hand side is "truthy", return that value (note that it is not converted to a boolean), otherwise, evaluate and return the right hand side. For &&, if the left hand side is "falsey", return it, otherwise evaluate and return the right hand side.

    Here are some examples:

    false && console.log("Nothing happens here");
    true || console.log("Or here");
    false || console.log("This DOES get logged");
    "foo" && console.log("So does this");
    
    if (obj && obj.property) // make sure obj is not null before accessing a property
    
    0 讨论(0)
  • 2020-12-01 14:50

    Single | is bit wise OR operator . If you do 2 | 3 , it converts to binary and performs OR operation. 01 11 Results in 11 equal to 3.

    Where as || operator checks if first argument is true, if it is true it returns else it goes to other operator. 2 || 3 returns 2 since 2 is true.

    0 讨论(0)
  • 2020-12-01 14:51

    In Javascript perspective, there is more to it.

    var a = 42;
    var b = "abc";
    var c = null;
    
    a || b;     // 42
    a && b;     // "abc"
    
    c || b;     // "abc"
    c && b;     // null
    

    Both || and && operators perform a boolean test on the first operand (a or c). If the operand is not already boolean (as it's not, here), a normal ToBoolean coercion occurs, so that the test can be performed.

    For the || operator, if the test is true, the || expression results in the value of the first operand (a or c). If the test is false, the || expression results in the value of the second operand (b).

    Inversely, for the && operator, if the test is true, the && expression results in the value of the second operand (b). If the test is false, the && expression results in the value of the first operand (a or c).

    The result of a || or && expression is always the underlying value of one of the operands, not the (possibly coerced) result of the test. In c && b, c is null, and thus falsy. But the && expression itself results in null (the value in c), not in the coerced false used in the test.

    0 讨论(0)
  • 2020-12-01 14:55

    To explain a little more in layman's terms:

    && and || are logical operators. This means they're used for logical comparison;

    if (a == 4 && b == 5)
    

    This means "If a equals to four AND b equals to five"

    | and & are bitwise operators. They operate on bits in a specific fashion which the wiki article explains in detail:

    http://en.wikipedia.org/wiki/Bitwise_operation

    0 讨论(0)
  • 2020-12-01 15:00

    | is a bitwise or, || is a logical or.

    A bitwise or takes the two numbers and compares them on a bit-by-bit basis, producing a new integer which combines the 1 bits from both inputs. So 0101 | 1010 would produce 1111.

    A logical or || checks for the "truthiness" of a value (depends on the type, for integers 0 is false and non-zero is true). It evaluates the statement left to right, and returns the first value which is truthy. So 0101 || 1010 would return 0101 which is truthy, therefore the whole statement is said to be true.

    The same type of logic applies for & vs &&. 0101 & 1010 = 0000. However 0101 && 1010 evaluates to 1010 (&& returns the last truthy value so long as both operands are truthy).

    0 讨论(0)
  • 2020-12-01 15:03

    one more point i want to add is || operator is used to assign default value in case the value you are assigning is undefined. So for Exapmle you are assigning a obj to some object test and if you dont want test to be undefined then you can do the following to ensure that value of test wont be undefined.

    var test = obj || {};

    so in case obj is undefined then test's value will be empty object.

    So it is also being used for assigning default value to your object.

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