-1

Possible Duplicate:
Wrong value in console.log
What |= does in JavaScript?

OK, I was reading an article on optimising JS for Googles V8 engine, when i saw this code example...

I nearly skimmed over it, but then I saw this; |=; a[0] |= b;

a = new Array();
a[0] = 0;
for (var b = 0; b < 10; b++) {
  console.log(a, b)
  a[0] |= b;  // Much better! 2x faster.
}

a[0] |= b;

So I ran it, in my console, with a console.log in the loop and resulted in 15;

[15] 0
[15] 1
[15] 2
[15] 3
[15] 4
[15] 5
[15] 6
[15] 7
[15] 8
[15] 9

WHAT?!?! Where the hell does it get 15 from, on every iteration?!?!?!

I've been a web dev for 7 years, and this has stumped me and a fellow colleague.

Can somebody talk me through this code?

Cheers.

Community
  • 1
  • 1
Will Hancock
  • 1,210
  • 4
  • 16
  • 27

3 Answers3

2

The reason it shows [15] in every iteration is because console.log() output is buffered, and objects or arrays passed to it are not evaluated immediately.

Your loop calculates 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9, which is 15.

By the time console.log() gets around to calling a.toString() the loop already finsihed, and so it shows that final result of 15, rather than the intermediate results.

If you had logged a[0] rather than a each time you would have seen the real result, because primitive values are evaluated immediately:

a = new Array();
a[0] = 0;
for (var b = 0; b < 10; b++) {
  console.log(a[0], b)
  a[0] |= b;  // Much better! 2x faster.
}

0 0
0 1
1 2
3 3
3 4
7 5
7 6
7 7
7 8
15 9
Alnitak
  • 313,276
  • 69
  • 379
  • 466
  • Yes, I verified that : http://jsfiddle.net/S3cE9/ (I was about to answer the same thing) – Denys Séguret Oct 12 '12 at 09:34
  • 1
    @dystroy please upvote - the rest of the answers have misread the question (although to be fair it was poorly written) – Alnitak Oct 12 '12 at 09:35
  • +1 done ([self promoting link included](http://stackoverflow.com/questions/11214430/wrong-value-in-console-log/11214508#11214508)) – Denys Séguret Oct 12 '12 at 09:36
  • +1 for the right answer to the question – Jacob George Oct 12 '12 at 09:36
  • Ok yeh, thats wrong, but doesn't anwer the question, what is |= and what does it do... – Will Hancock Oct 12 '12 at 11:19
  • Ok, I see the confuion, the question got renamed?!?! I Originally called "What |= does in JavaScript?" I don't know who changed it to "Why does this log output show the same answer in each iteration?" – Will Hancock Oct 12 '12 at 11:22
  • @WillHancock I changed the question title, because the _real_ question was your line _"Where the hell does it get 15 from, on every iteration?"_, which is explained by the `console.log()` behaviour, and not _really_ how `|=` works, which should be obvious to a webdev with your experience ;-) – Alnitak Oct 12 '12 at 11:52
  • p.s. your downvote it not appreciated. Mine was the only answer to address your real point of confusion (i.e. why the answer was apparently 15 every time). – Alnitak Oct 12 '12 at 12:03
1

| is bitwise OR more info here

Vimalnath
  • 6,202
  • 2
  • 24
  • 45
0
  a[0] |= b; 

is equal to

  a[0] =  a[0]| b;  //bit wise operation
swapnesh
  • 24,280
  • 22
  • 88
  • 122