0

I am using Google Closure and LOVE conditional assignments. I regularly use the

var stuff = (condition) ? opt0: opt1;

method and have more recently been introduced to

var stuff = opt_param || defaultValue;

But I have noticed a strange behavior, since it seems the

var stuff = opt_param | defaultValue;

is aslso working (i.e. with the single |), but this appears to "type cast" the variable into a number. I have not been able to find any documentation on this, so here's my question:

What is the difference between the single | and the double || in terms of logic and assignment? And is my assumption regarding the number type cast correct, or just an arbitrary product of the special case(s) I've been fiddling with?

NinjaTool
  • 63
  • 1
  • 5
  • 1
    I don't see how this question is related to the closure tools. – Felix Kling Apr 12 '13 at 09:44
  • possible duplicate of [what's the difference between ( | ) and ( || ) in javascript?](http://stackoverflow.com/questions/5690512/whats-the-difference-between-and-in-javascript) – Felix Kling Apr 12 '13 at 09:45
  • You are absolutely right, Felix. I only thought it might, since the closure tools previously have been the only ones to tell me when I messed up in relation to "types"... Also; As far as I've been able to figure, it's not possible to get any results on special characters like the |-pipe, so that might be why I had missed the previously posted question. My bad. – NinjaTool Apr 15 '13 at 10:51
  • No worries :) Since the introduction of a new search engine, you can search for special characters by putting them in quotes, e.g. `[javascript] "|" "||"`. – Felix Kling Apr 15 '13 at 10:59
  • Fantabulous! Will definitely be using that...! Thanks. – NinjaTool Nov 26 '13 at 09:59

3 Answers3

1

| is bitwise OR:

3 | 4 = 7
0xFF00FF | 0x00FF00 = 0xFFFFFF

|| is logical OR, evaluates to the first argument if it is true, otherwise the second:

3 || 4 = 3
0 || 4 = 0
Koterpillar
  • 7,883
  • 2
  • 25
  • 41
1

Briefly speaking, a single | refers to bitwise OR operator, which "performs the OR operation on each pair of bits. a OR b yields 1 if either a or b is 1". Example from MDN:

     9 (base 10) = 00000000000000000000000000001001 (base 2)
    14 (base 10) = 00000000000000000000000000001110 (base 2)
                   --------------------------------
14 | 9 (base 10) = 00000000000000000000000000001111 (base 2) = 15 (base 10)

While || is a logical OR that evaluates the first non-falsy statement in the assignment:

  123 || 0      === 123
false || "Text" === "Text"
   "" || true   === true

So basically these two operators are not interchangeable and may result in different values:

10 | 2  === 10
10 || 2 === 10

... however,

9 | 14  === 15
9 || 14 === 9
VisioN
  • 143,310
  • 32
  • 282
  • 281
0

The || operator is short-circuit OR. It assigns the first if it is true-like or the second. | on the other hand is Bit-wise operator. It performs a bitwise OR on the two parameters. If the first one evaluates to 0 result of bit-wise OR will be the second parameter.

It will give wrong results when both are numbers. Better go with ||.

Please have a look at Bitwise Operators on MDN.

ManojRK
  • 952
  • 9
  • 22