6

I was wondering if there is a "&" logical operator in Javascript. I tried running 1 & 0 and 1 && 0 in Firebug(Firefox) and it returned a 0 for both.

Someone told me that C# accepts both & and double &&, double being more efficient as it will exit the comparison loop as soon as a false is encountered, but I was not able to find any info for Javascript on that.

Any ideas?

DLS
  • 5,003
  • 5
  • 35
  • 49

8 Answers8

10

No. & is a bitwise AND operator. && is the only logical AND operator in Javascript.

Josh Stodola
  • 77,975
  • 43
  • 178
  • 222
  • 2
    It's *like* a logical "AND" operator, but unlike other languages it does not produce a boolean result value unless its operands are boolean. – Pointy Jul 30 '10 at 19:59
5

The && operator returns 0 for the expression 1 && 0 because its semantics are different than those of the same operator (well, symbolically the same) in other C-like languages.

In Javascript, the && operator does coerce its operands to boolean values, but only for the purposes of evaluation. The result of an expression of the form

e1 && e2 && e3 ...

is the actual value of the first subexpression en whose coerced boolean value is false. If they're all true when coerced to boolean, then the result is the actual value of the last en. Similarly, the || operator interprets an expression like this:

e1 || e2 || e3 ...

by returning the actual value of the first en whose coerced boolean value is true. If they're all false, then the value is the actual value of the last one.

Implicit in those descriptions is the fact that both && and || stop evaluating the subexpressions as soon as their conditions for completion are met.

Pointy
  • 371,531
  • 55
  • 528
  • 584
2

1 & 0 is 0.

It's a bitwise operator, not a logical operator.

&& means a logical AND of the left and right operators. This means it will return a boolean true value only if both the left and right operators resolve to boolean true.

& means a bitwise AND of the left and right operators. This means the bits of each operand will be compared and the result will be the ANDed value, not a boolean. If you do 101 & 100 the return value is 100. If you do 1 & 0, the return value is 0.

You've been mislead about the meaning of the two operators if someone told you the difference was just in efficiency. They have very different uses.

Jonathon Faust
  • 11,876
  • 3
  • 47
  • 61
  • That's not exactly right about `&&`. The expression `"hello" && 0` returns `0`, not `false`, and the expression `"hello" && "world"` returns "world", not `true`. – Pointy Jul 30 '10 at 19:52
  • I understand JavaScript behaves different than what I described, but I was more concerned with the distinction between logical and bitwise AND (and how they behaves in "most" languages). – Jonathon Faust Jul 30 '10 at 20:07
  • Right on both accounts. In any case, it may be worth noting that JavaScript's `&&` and `||` operators return *truthy/fasley* values, not necessarily booleans. They also act like C#'s coalesce operator `??` http://msdn.microsoft.com/en-us/library/ms173224.aspx `&&` will return the first *falsey* value or the last *truthy* value; `||` will return the first *truthy* value. – Justin Johnson Jul 30 '10 at 20:12
2

Your friend is wrong about C#.

C# does not mix logical and bitwise operators, so you cannot use & where && is needed or vice versa.

1 & 0 returns 0

true && false returns false

So if you're writing an if statement, which expects a boolean, you have to use &&. And if you're doing bit-wise arithmetic, then you need &.

Neil
  • 6,747
  • 4
  • 40
  • 42
  • Interesting. Java does allow boolean operations with `&` and `|`, and in that context they exist explicitly to avoid short-circuiting. – Pointy Jul 30 '10 at 19:55
1

Yes. Javascript has both. http://www.eecs.umich.edu/~bartlett/jsops.html

Exactly as is true in C#, the double && version can stop as soon as it encounters a false, and the single & version may not.

sblom
  • 25,623
  • 4
  • 65
  • 95
1

You can check this for bitwise in javascript:

https://www.w3schools.com/js/js_bitwise.asp

& is Sets each bit to 1 if both bits are 1

Examples: 5 & 1 = 1

0101 & 0001 = 0001

Haryono
  • 700
  • 8
  • 10
0

&& is the logical operator in Javascript. 1 && 0 should return false so it is performing correctly.

dave
  • 11,282
  • 8
  • 39
  • 58
0

Javascript has the bitwise (&) and boolean (&&) operators. The reason && returns a 0 on 1 && 0 is because 0 would indicate false and so 1 (true) && 0 (false) returns a false as both operators must evaluate to true to return a true

  • But why return "0" instead of "false" ? – James Curran Jul 30 '10 at 19:51
  • This is right but it doesn't return `false`, it returns the actual value of the first expression that, when coerced to boolean, is `false`. – Pointy Jul 30 '10 at 19:52
  • @James Curran because `&&` and `||` in Javascript are **different** than the similar operators in C and Java etc. They do not return a boolean result unless working with boolean subexpressions. – Pointy Jul 30 '10 at 19:53