Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

Furthermore, what's the difference between & and &&?

like image 940
Buffon Avatar asked Apr 17 '11 00:04

Buffon


People also ask

What is difference between and || in C?

The | operator evaluates both operands even if the left-hand operand evaluates to true, so that the operation result is true regardless of the value of the right-hand operand. The conditional logical OR operator ||, also known as the "short−circuiting" logical OR operator, computes the logical OR of its operands.

What is || used for?

We use the symbol || to denote the OR operator. This operator will only return false when both conditions are false. This means that if both conditions are true, we would get true returned, and if one of both conditions is true, we would also get a value of true returned to us.

What does || in coding mean?

Logical OR operator: || The logical OR operator ( || ) returns the boolean value true if either or both operands is true and returns false otherwise.

What's the difference between and || in JS?

The main difference is that nullish coalescing(??) operator will only give the result as the right operand only if the left operand is either null or undefined . Whereas the OR(||) operator will give the result as right operand for all the falsy values of the left operand.


2 Answers

| 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).

like image 56
Chris Eberle Avatar answered Oct 07 '22 18:10

Chris Eberle


  • & is the bitwise AND operator

  • | is the bitwise OR operator

  • && is the logical AND operator

  • || is the logical OR operator

The difference is that logical operators only consider each input at face value, treating them as whole, while bitwise operators work at the bit level:

var thetruth = false; var therest = true;  var theuniverse = thetruth && therest; //false var theparallel = thetruth && thetruth; //true  var theindifferent = thetruth || therest; //true var theideal = thetruth || thetruth; // false  var thematrix = 5346908590; var mrsmith = 2354656767;  var theoracle = thematrix & mrsmith; //202445230 var theone = thematrix | mrsmith; //7499120127 
like image 38
Grant Thomas Avatar answered Oct 07 '22 18:10

Grant Thomas