1

I ask about it because if you try it in Java:

boolean a = true, b = false;
boolean c = a | b;

Then in c variable will be true value. But if we run similar code in JS:

var a = false, b = true;
var c = a | b;

Then in c variable will be 1 value. How to explain this strange behavior?

zegoline
  • 534
  • 1
  • 7
  • 21
  • Java is not JavaScript. – Grijesh Chauhan May 07 '15 at 07:08
  • 1
    I'd guess it's something to do with you using the bit-wise operator. – Ash Burlaczenko May 07 '15 at 07:08
  • 1
    `var c = a || b` and see. You need to refer [bitwise OR](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#.7C_(Bitwise_OR)) ,[logical OR](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Logical_OR_(.7C.7C)) – tkay May 07 '15 at 07:09
  • Why is it strange? What are you expecting it to be and why? – charlietfl May 07 '15 at 07:11
  • Zegline their is also type conversion `boolean c = 1` gives `true` in Java whereas `var c = 1` gives `1` in JavaScript, So you are confused because of variable type and implicit type conversion in Java, Otherwise `false | true` is one in both languages as you are applying bitwise operator - both languages are different. – Grijesh Chauhan May 07 '15 at 07:15

2 Answers2

1

| is a bitwise operator. When you write “false | true” then it is treated like 0|1.

From MDN:

Performs the OR operation on each pair of bits. a OR b yields 1 if either a or b is 1. The truth table for the OR operation is:

a   b   a OR b
0   0   0
0   1   1
1   0   1
1   1   1

Also as correctly commented by Grijesh, that in Java boolean c = 1 gives true whereas in Javascript var c = 1 gives 1 due to the type conversion. The implicit type conversion happens in Java but not in Javascript.

Here is a good article to read: All about types in Javascript – Automatic type conversion

The logical operators in javascript work on any type, unlike in Java. And unlike in Java, they do not return a boolean value. Still they expect boolean values and if not given two the operands are converted to boolean.

Rahul Tripathi
  • 152,732
  • 28
  • 233
  • 299
0

|| means OR and returns true.

true || false; // true
false || true; // true

| is actually a bit-wise operator that interprets it like 0|1 and returns the number 1 because bit-wise operators can only work on numbers.

false | true; // 1
0 | 1;        // 1

true | false; // 1
1 | 0;        // 1
Sebastian Simon
  • 14,320
  • 6
  • 42
  • 61
  • I think you should also add in JavaCode `c` is a boolean type (that is why it was `true`) whereas in JavaScript type is dynamic ( and value `1` doesn't converted to bool type in JS) – Grijesh Chauhan May 07 '15 at 07:10
  • @GrijeshChauhan That has very little to do with the reason. The main point is that `|` is a bit-wise operator. – Sebastian Simon May 07 '15 at 07:11
  • No, every one missing this but that is first point (that is invisible easyly), - type conversion performed in Java code but not in JS – Grijesh Chauhan May 07 '15 at 07:12
  • Oh, okay, because in Java it’s `true`. But that’s only because you’ve specified `c` as being a `boolean`. By default `true | false` still returns a number but is implicitly converted to a boolean by Java. – Sebastian Simon May 07 '15 at 07:18
  • yes, that is the reason, I have added a comment regarding this to question. You should go further and explain your answer. – Grijesh Chauhan May 07 '15 at 07:19