-1

What does it mean?

1.

a==b&&b={}

2.

a==b||b={}

I didn't find answer

user1758424
  • 87
  • 1
  • 8

2 Answers2

1

It means, if a equals b, then set b to an empty object. The second one means more or less the same. The difference is that the first one will not set b to an empty object if a and b are not equal. But the second one will always do that no matter what. That is because the OR operator in javascript continues where as the AND operator will short-circuit.

http://www.openjs.com/articles/syntax/short_circuit_operators.php

Mohamed Nuur
  • 5,056
  • 6
  • 34
  • 52
  • It only works when you put parenthesis around the assignment portion, otherwise due to the order of operations, it will fail. – Mohamed Nuur Nov 04 '12 at 00:05
1

a==b is the condition to be tested. The operators && and || test the condition very much like a ternary operator but you use it when there's only one condition you need to test, either false || or true &&. It would be the same as:

if ( a == b ) { b = {} } // a == b && ( b = {} )
if ( a != b ) { b = {} } // a == b || ( b = {} )

But as Esailija pointed out in the comments, seems like you're missing some parenthesis:

a == b || ( b = {} )
elclanrs
  • 85,039
  • 19
  • 126
  • 159
  • Why I need to add parenthesis? And what is so bad in my question? – user1758424 Nov 04 '12 at 00:17
  • The parenthesis are needed because of the [operator precedence](http://www.scriptingmaster.com/javascript/operator-precedence.asp). Parenthesis are _always_ evaluated first since they have highest precedence. The assignment operator `=` has lower precedence than the or operator `||`. – elclanrs Nov 04 '12 at 00:24