This content originally appeared on DEV Community and was authored by Cornelia
Sometimes I like to take a look at the quirkiness of the language I code in and compare it to other languages. This time, I thought I'd share my findings with you, in case you also find them entertaining and interesting. On this occasion I looked at a quirk featured in WTFJS, namely doing Math with booleans. Let's dive in.
true + true = ?
In Javascript, we use the + operator to concatenate strings as well as to add up numbers, but what happens when we through some booleans into the mix? Let's try!
'Hi' + true; // output: "Hitrue"
true + 'Hi'; // output: "trueHi"
Okay, so far, so good, Javascript notices that we use a string with a + operator and treats the boolean like a String and concatenates the two. Now, let's take a look at the numbers.
5 + true; // output: 6
true + 5; // output: 6
Wait, what? Let's take a closer look at what's happening here.
the Number-function
The boolean value is passed to the Number
-function, which converts it to a Number; true
is converted to 1 and false
to 0.
Note
The Javascript Number type has limits to what it can store and also limits on how high the numbers stored in can be. All Numbers are stored as floating-point values.
If it fails to convert the value into a number, it will return NaN. It can convert true, false, null, as well as a decimal and hexadecimal number (in strings).
Number(true); // output: 1
Number(false); // output: 0
Number(null); // output: 0
Number(undefined); // output: NaN
Number(0x7E5); // output: 2021
Number('0x7E5'); // output: 2021
Number('12.5'); // output: 12.5
calculation with booleans only
When we only add boolean operands, Javascript will still try and convert them into Numbers, rather than interpreting both as being strings.
true + true; // output: 2
true + false; // output: 1
false + false; // output: 0
Beyond the addition
The same effect, of course, happens with subtractions, multiplications and divisions, as well as when using unary operators (positive and negative - although NaN will not be signed).
+true // output: 1
+false // output: 0
+null // output: 0
+undefined // output: NaN
+0x75E // output: 2021
+'0x75E' // output: 2021
+'12.5' // output: 12.5
Is it just JS
You might know all of this already, so let's compare it to a few other languages.
PHP and Python will also allow calculations with booleans. They both also convert true
to 1 and false
to 0.
Java and Rust on the other hand, will both refuse calculations with boolean values and throw an error during compilation.
error: bad operand types for binary operator '+'
Javaerror [E0277]: cannot add
bool
to{integer}
Rust
I hope you had fun reading about calculations with booleans. Thanks for reading!
This content originally appeared on DEV Community and was authored by Cornelia
Cornelia | Sciencx (2021-08-18T21:00:15+00:00) Calculations with booleans. Retrieved from https://www.scien.cx/2021/08/18/calculations-with-booleans/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.