JavaScript Interview Question #18: What's the sum of two booleans in JavaScript?
Coderslang: Become a Software Engineer
Posted on February 22, 2021
Can you add booleans in JS? Is something false
here? What will be logged to the screen?
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Just as in the previous test, we’re dealing here with type conversion
and loose equality
using the ==
operator.
When JavaScript evaluates the expression true + true
it first converts booleans to numbers, which is 1
for true
and 0
for false
.
When we try to do calculate the value of 2 == true
, the typecast happens again and we arrive at the final condition 2 == 1
.
The result is obviously false, so we go into the else
branch.
To understand how type conversion works with the +
operator and different data types, you can read this article.
ANSWER: the string everyone is different after all
will be logged to the console.
Posted on February 22, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
February 22, 2021