# Operator && doesn't work, unbelievable

**URL:** https://discourse.nodered.org/t/operator-doesnt-work-unbelievable/6843
**Category:** General
**Created:** [15 January 2019 14:00 UTC](https://discourse.nodered.org/t/operator-doesnt-work-unbelievable/6843 "2019-01-15T14:00:53Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![GiovanniG](https://avatars.discourse-cdn.com/v4/letter/g/ecd19e/32.png) [@GiovanniG](https://discourse.nodered.org/u/GiovanniG)
#### Post date: [15 January 2019 14:00 UTC](https://discourse.nodered.org/t/operator-doesnt-work-unbelievable/6843/1 "2019-01-15T14:00:53Z")

</div>

Hi everone, I have this semplified code, I omiss the list of commands, ony the condition:

var IO1\_0 = 1;  
if (IO1\_0 && 0) I have false, ok  
if (IO1\_0 && 1) I have true ok  
if (IO1\_0 && 2) I have true WHY?  
if (IO1\_0 == 2) I have false, ok  
if (IO1\_0 && 0x80) I have also true, WHY?

Thank you

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [15 January 2019 14:11 UTC](https://discourse.nodered.org/t/operator-doesnt-work-unbelievable/6843/2 "2019-01-15T14:11:47Z")

</div>

It helps to look at what values each of those statements is generating. I suspect the `&&` operator in JavaScript is not what you think it is.

From [MDN's description of logical operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators):

> `expr1 && expr2` means:  
> If `expr1` can be converted to `true` , returns `expr2` ; else, returns `expr1` .

So for each of your statements, here's what the expression returns:

```auto
(1 && 0) == 0
(1 && 1) == 1
(1 && 2) == 2
(1 == 2) == false
(1 && 0x80) == 128

```

When treating a number as a boolean, `0` is considered false and any other number is true - hence the results you get.

---

<div class="post-metadata">

### Author: ![GiovanniG](https://avatars.discourse-cdn.com/v4/letter/g/ecd19e/32.png) [@GiovanniG](https://discourse.nodered.org/u/GiovanniG)
#### Post date: [15 January 2019 14:18 UTC](https://discourse.nodered.org/t/operator-doesnt-work-unbelievable/6843/3 "2019-01-15T14:18:55Z")

</div>

Thank you a lot, sincerely I don't know why they gave that meaning to it, I?ve lost almost an hour, I was thinking it was something else..  
I solved in this way: if ((IO0\_0 & 0x80)==0x80)

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [15 January 2019 16:26 UTC](https://discourse.nodered.org/t/operator-doesnt-work-unbelievable/6843/4 "2019-01-15T16:26:30Z")

</div>

> [@GiovanniG](#):
>
> I don't know why they gave that meaning to it, I

I believe that most languages have that meaning for the operator &&

---

<div class="post-metadata">

### Author: ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)
#### Post date: [15 January 2019 20:31 UTC](https://discourse.nodered.org/t/operator-doesnt-work-unbelievable/6843/5 "2019-01-15T20:31:39Z")

</div>

> [@Colin](#):
>
> I believe that most languages have that meaning for the operator &&

Please show a bit more sympathy for us refugees from C/C+/C#. Overloading of logical operators is standard in JavaScript but not automatic elsewhere. Correct me if I'm mistaken, but I believe that in most dialects of C, `&&` can be overloaded by the user only if `&` is overloaded and at least some type checking is disabled -- not something that is done routinely.

---

<div class="post-metadata">

### Author: ![GiovanniG](https://avatars.discourse-cdn.com/v4/letter/g/ecd19e/32.png) [@GiovanniG](https://discourse.nodered.org/u/GiovanniG)
#### Post date: [15 January 2019 21:15 UTC](https://discourse.nodered.org/t/operator-doesnt-work-unbelievable/6843/6 "2019-01-15T21:15:51Z")

</div>

Sorry mates, would better I argue more with the Javascript guide I read, they gave the same meaning I was addicted from C.. and I believed them.  
There is any other symbol like && for C? ANd what about ||? I saw it was used to inizialize a variable if it is the first time declared 😮 propbably if it property is "udefined"?  
Thank you

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [15 January 2019 21:19 UTC](https://discourse.nodered.org/t/operator-doesnt-work-unbelievable/6843/7 "2019-01-15T21:19:15Z")

</div>

Actually, I take it back, && isn't the same as C at all as it returns one of the original values rather than true or false.  
However, going back to your original question, the expressions you were not happy with were  
`1 && 2` which tests true and  
`1 && 0x80` which also tests true.  
I believe I am right in saying that in C both of those would also return true. It is a long time since I used C however, so I may be wrong here too. I have just looked in my well thumbed 1978 version of Kernighan and Ritchie and I think that is right.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [15 January 2019 21:33 UTC](https://discourse.nodered.org/t/operator-doesnt-work-unbelievable/6843/8 "2019-01-15T21:33:15Z")

</div>

> [@GiovanniG](#):
>
> There is any other symbol like && for C? ANd what about ||? I saw it was used to inizialize a variable if it is the first time declared 😮 propbably if it property is "udefined"?

`||`, logical OR is also very similar to that in C. If either of the values can be considered truethy then the result will also be truethy. It can be used in initialising because undefined is falsey so, for example,  
`someValue || 0` will give 0 if someValue is undefined, as undefined is considered falsey.

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [15 January 2019 22:06 UTC](https://discourse.nodered.org/t/operator-doesnt-work-unbelievable/6843/9 "2019-01-15T22:06:04Z")

</div>

> [@GiovanniG](#):
>
> ANd what about ||?

The MDN page I linked to has the definition of `||` as well:

> `expr1 ||expr2`  
> If `expr1` can be converted to `true` , returns `expr1` ; else, returns `expr2` .

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [16 January 2019 09:10 UTC](https://discourse.nodered.org/t/operator-doesnt-work-unbelievable/6843/10 "2019-01-16T09:10:14Z")

</div>

I have just run some tests to check what happens with C to compare with Javascript. The conclusion is that the `if` statements

```auto
if (a && b) 
if (a || b) 

```

give the same results in js and C.  
The difference arises with

```auto
int i = a && b;
int j = a || b;

```

and

```auto
var i = a && b;
var j = a || b;

```

where C always returns 1 or 0 whereas js returns either a or b (but both will be interpreted the same when used in an `if` statement.  
So for the examples @GiovanniG gave at the start, the results are the same in C or js.

---

<div class="post-metadata">

### Author: ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)
#### Post date: [16 January 2019 20:49 UTC](https://discourse.nodered.org/t/operator-doesnt-work-unbelievable/6843/11 "2019-01-16T20:49:22Z")

</div>

Nice work, @Colin. Old-fashioned C (before C99) secretly wanted the arguments and result of `&&` to be boolean, but did not have a boolean data type. It used (integer) 0 for `false` and anything non-zero interpreted as `true`. Later versions and dialects have boolean data types but maintain backward compatibility. @GiovanniG missed the point that 2 and 0x80 are `true` in this context.
