-3

I want to know the value of result in the following two cases:

1.

var result = false;
result |= false;

2.

var result = false;
result |= true;
Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
  • Look up what `console.log(result);` does, then perform the experiment. See green's answer for why. – Bob Brown Oct 30 '14 at 02:25
  • 2
    You probably wouldn't have the downvotes if you hadn't phrases the body of your question in a way that can be totally answered by just running your samples in the console. Your actually interesting question is only asked in the title. – Sam Hanley Oct 30 '14 at 02:27

3 Answers3

3

it's the BitWise OR Assignment. See http://msdn.microsoft.com/en-us/library/ie/81bads72%28v=vs.94%29.aspx

so result |= expression is the same as result = result | expression

totoro
  • 3,257
  • 5
  • 39
  • 61
1

The | operator is the Bitwise OR operator. Rather than doing a standard binary OR (truth table below) the | operator will perform a binary OR for each bit of both operands.

Binary OR

x|y|q
0|0|0
1|0|1
0|1|1
1|1|1

Example

3 | 5
// in binary
0011 | 0101
// as a truth table

x|y|q
0|0|0
0|1|1
1|0|1
1|1|1

0011 | 0101 = 0111
// in decimal
3 | 5 = 7

Using the Bitwise OR operator in conjunction with an equals sign turns it into an assignment statement, in a similar way to += or -= or any of the other logic/arithmetic operators. It is just a shorthand for result = result | operand.

It's important to note that if you use the operator with boolean values (true or false), they are coerced into numeric values (1 and 0, respectively) before being compared in the above way.

Dan Prince
  • 29,491
  • 13
  • 89
  • 120
0

It is an Assignment Operator of a BitWise Operator "|" which returns a one in each bit position for which the corresponding bits of either or both operands are ones.

See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Arithmetic_operators

Litestone
  • 529
  • 7
  • 22