Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boolean addition in Javascript

How do i do a clean boolean add in javascript?

1+1 = 0;
1+0 = 1;
1+1+1 = 1;

etc. can one just sum booleans?

true+true = false
false+true = true;

etc.

like image 368
David Karlsson Avatar asked Jun 24 '26 20:06

David Karlsson


1 Answers

Just use bitwise XOR operator:

1 ^ 1 = 0
1 ^ 0 = 1
1 ^ 1 ^ 1 = 1

FWIW: The same works for most high-level programming languages.

like image 57
VisioN Avatar answered Jun 27 '26 08:06

VisioN



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!