0

Possible Duplicate:
what's the difference between ( | ) and ( || ) in javascript?

I've seen this in a couple examples here but I never fully understood what it's supposed to do. Can anyone give me a simple example please?

Community
  • 1
  • 1
0x499602D2
  • 87,005
  • 36
  • 149
  • 233

2 Answers2

2

In Javascript, the | operator is a bitwise operators (in contrast to the || operator which is a logical operator).

It convert each operand to a 32-bit number, and performs a bitwise or between them.

Example of expressions and their results:

1 | 1 === 1
1 | 2 === 3
1.99 | 2.99 === 3

Reference: http://developer.mozilla.org/en/JavaScript/Reference/operators/bitwise_operators

Guffa
  • 640,220
  • 96
  • 678
  • 956
  • 1
    May I politely ask why you feel the need to litter our garden when _we're_ busy trying to tidy it? – Grant Thomas Aug 13 '11 at 16:47
  • 2
    I don't speak for @Guffa, but IMO, giving a good answer is never "litter", irrespective of the existence of duplicate questions. – user113716 Aug 13 '11 at 16:49
  • 1
    @Mr. Disappointment: What on earth are you talking about? Besides, you failed miserably in asking politely... – Guffa Aug 13 '11 at 16:49
  • @patrick dw: The 'litter' aspect comes into it when an answer is supplied to a redundant question, it wouldn't necessarily be litter in a more distinguished question to which is still relates. – Grant Thomas Aug 13 '11 at 16:50
  • 1
    ...IMO, giving a good answer is never "litter"... – user113716 Aug 13 '11 at 16:53
2

Depends where you use it:

Community
  • 1
  • 1
Krule
  • 6,398
  • 3
  • 32
  • 53