1

I know that in Java if I write condition1 & condition2, Java will check both conditions and even if condition1 is already false, Java will check condition2 and then decide.

On the other hand when writing condition1 && condition2 if condition1 is false it wont check condition2. Does it work the same in JavaScript/TypeScript?

Ivar
  • 4,655
  • 12
  • 45
  • 50
shani klein
  • 172
  • 1
  • 11
  • 2
    Yes, `&&` short circuits, if that is what you are after. [There are other differences, though](https://stackoverflow.com/questions/4535647/logical-operators-in-javascript-how-do-you-use-them) - the most prominent is that the *result* of the boolean operation `a && b` will not necessarily be a *boolean*. It will return a truth**y** or a false**y** value from that check. – VLAZ Sep 09 '19 at 12:18
  • 1
    @Ivar I believe OP is asking if `&&` in TypeScript (and JavaScript, since that's what TS produces) acts like `&&` in Java. The example given is exactly how Java treats the operators - `&&` short circuits and `a && b` doesn't evaluate `b` if `a` is `false`. On the other hand `a & b` will evaluate both regardless of the value of `a` – VLAZ Sep 09 '19 at 12:25
  • "_Java will check both conditions and even if condition1 is already false java will check condition2 and then decide_". Sure it evaluates both booleans, but there isn't much to decide. If condition 1 is false. the outcome will always be false. Condition 2 can never change that. – Ivar Sep 09 '19 at 12:38
  • @Ivar well, that is correct but also the `&` operator in Java will do it regardless. So, if you do something like `isUserLoggedIn() & calculateSomething()` you can be sure that the calculation will be done in all cases. It's probably not a great idea, honestly but [it's how `&` works](https://stackoverflow.com/questions/5564410/what-is-the-difference-between-and-in-java) – VLAZ Sep 09 '19 at 12:40
  • @VLAZ I'm aware, I was referring to the "_and then decide_". – Ivar Sep 09 '19 at 12:45
  • @Ivar the only situation this *might* be useful is if the methods you call have side effects and you want all of them to execute regardless of their return values. Again, that's not usually a very good idea and it will most definitely be slower than *not* evaluating anything after a `false` (and conversely, if you use OR, then you can skip any evaluation after you get any `true` value) but it's how the language was designed. From my experience, a single `&` or `|` is pretty much always a mistake. I only had *one* occasion where it was useful and I think I should have avoided it. – VLAZ Sep 09 '19 at 12:48
  • @VLAZ I appreciate the effort you take to explain it, but I'm well aware of how both bitwise and logical operators work in both Java and JS/TS. – Ivar Sep 09 '19 at 12:51
  • @Ivar It’s reasonable to assume the author of the question wishes to find out not only the differences and similarities between the operator `&&` behaviors in Java and Typescript but also how to eliminate the difference. Using double exclamation mark is relevant enough to be mentioned here (see my answer), it helps to bring the behavior in Typescript closer to that in Java. Please note it is not mentioned in the earlier question which you believe makes this one a duplicate. – winwiz1 Sep 09 '19 at 14:20
  • @winwiz1 You can see that "Community" also marked the question as duplicate and that only happens when OP themselves accepts the proposed duplicate. I personally don't really see why that is important. If based on the information OP wonders how you can get a boolean instead of a truthy/falsy value, then that could be a separate question, which already has other duplicates here on SO. – Ivar Sep 09 '19 at 14:28

2 Answers2

1

& and && are separate and different operators in Javascript (and hence Typescript).

In Javascript, & is the bitwise and, for example:

5 & 13 & 3 === 1; // 0b0101 & 0b1101 & 0b0011 = 0b0001

&& is the logical and and shorthands the same way as in other languages. However, beware of type coercion as Javascript has many surprises about what evaluates to a truthy or falsy value.

5 < 3 && foo(); // foo is NOT called
[] && foo(); // foo is called
"" && foo(); // foo is NOT called
Etheryte
  • 20,940
  • 10
  • 58
  • 98
  • Note the "Java" in the questions title. – Adder Sep 09 '19 at 12:31
  • @Adder I don't get how that would affect the answer. – VLAZ Sep 09 '19 at 12:37
  • There should be at least a few lines explaining that the operators work the same in Java. Also maybe in what they are different in Javascript (returning truish but not true values) – Adder Sep 09 '19 at 12:40
  • 1
    @Adder but they *don't* work the same in Java. Not entirely. And OP seems like they already know how the Java operators work, so this answer discusses how they work in JS - `&` does not do logical evaluation but is only bitwise arithmetic. And the difference with `&&` is that it checks for truthy and falsey values - the answer points out resources on that and examples of what that means. – VLAZ Sep 09 '19 at 12:43
  • Just saying `console.log(false || [1,3]);` – Adder Sep 09 '19 at 13:11
  • 1
    @Adder I'm not sure what that line is intended to communicate that isn't already contained in the answer. – Dave Newton Sep 09 '19 at 13:43
  • It evaluates to `[1,3]`. This info is not in the answer unless you count linking to the documentation and saying `read this` as an answer. – Adder Sep 09 '19 at 13:47
  • @Adder The question was solely about `&` and `&&` so I don't see your point. – Etheryte Sep 09 '19 at 18:30
  • It also applies to `console.log(true && [1,3]);` – Adder Sep 10 '19 at 07:55
  • @Adder Still don't see how that's relevant. The question is about which conditions are checked, not the return value. – Etheryte Sep 10 '19 at 08:20
1

Typescript (and Javascript) operator && differs from its Java counterpart. Consider this code:

example.ts
-----------
const b = true;
const str = "abc";

const result = b && str;   // the operator '&&` returns string
const t = typeof result;
console.log(t)             // prints 'string'

To see boolean printed in the console, you need to replace b && str with b && !!str.

winwiz1
  • 1,759
  • 5
  • 18