-8
var a = 0;
var c = 3;

function myFunction(b) {
    a = a | b;
    return (a == c);
}

Saw this today, what does "a = a | b" do?

Jin
  • 39
  • 1
  • 5
  • 5
    this has to be a duplicate, search for "bitwise or". – zzzzBov Jun 04 '13 at 18:58
  • 1
    [Javascript operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators) – Martin Smith Jun 04 '13 at 18:59
  • I think you should learn some very (very) basic javascript before asking such questions. but it mean (a or b), where an integer is true if it's different than 0. – Gal Jun 04 '13 at 18:59
  • 5
    I really don't understand all of the downvotes here, sure it is a duplicate but it is very hard to get good search results on `|`. – Andrew Clark Jun 04 '13 at 19:01
  • 3
    @Gal - *ahem*, be careful who you chastise about 'basic javascript'. `|` is the bitwise, not the logical operator – Dancrumb Jun 04 '13 at 19:01
  • 1
    @F.J - But it is very easy to google "Javascript operators" then `CTRL+F` look for `|` – Martin Smith Jun 04 '13 at 19:01
  • 2
    @F.J - the downvotes probably come from the fact that "This question does not show any research effort" – Dancrumb Jun 04 '13 at 19:02
  • I'm so sorry for the duplicate, but like @F.J said, I searched around for this, but it is really hard to find good search results with |. – Jin Jun 05 '13 at 07:49
  • @MartinSmith I did exactly that. It's not in http://www.w3schools.com/js/js_operators.asp nor http://www.w3schools.com/js/js_comparisons.asp Anyway, thanks for those who provided the helpful answers, and for those who down voted this, try search yourself if u think its easy to look for symbols like this which are used in many pages as separators. – Jin Jun 05 '13 at 07:54
  • @Jin - As a rule of thumb always skip the w3schools links and go straight to MDN. – Martin Smith Jun 05 '13 at 08:01
  • @gal you should read the question properly, it is | not ||. – Jin Jun 05 '13 at 08:05
  • @martinsmith noted with thanks. – Jin Jun 05 '13 at 08:06

1 Answers1

2

You are doing bitwise-or operation and asigning the result to a.

Example:

if a=5 and b=4 then corresponding bits of their binary representation is operated by an or-operation.

    a=101
    b=100

    a=a|b=101|101=101=5;
pinkpanther
  • 4,462
  • 2
  • 33
  • 57