Predict the output of the following javascript code
console.log(true + false);
Quiz Explanation
In JavaScript, when you perform arithmetic operations with values of different types, JavaScript tries to coerce the values into numbers before performing the operation. Here, true
and false
are coerced to their numeric equivalents, which are 1
and 0
respectively.
So, the expression true + false
is equivalent to 1 + 0
, which equals 1
. Therefore, the output of the code will be:
1
JavaScript treats true
as 1
and false
as 0
when performing arithmetic operations like addition.