Question #51

Author: admin
tags: JavaScript  
const x = (true + true + '0') - (typeof NaN).length;
console.log(x); // ?
What will the console output be?
Nothing. There will be an error.
undefined
'truetrue0-3'
-1
-3
-4
-6
-10
-14
4
14
104
107
First, the interpreter parses true + true.
These operands are not strings, so only addition is performed, in mathematical operations true and false automatically become 1 and 0.
Therefore, if you add true + true, you get 2.
The + operator is used for both addition and concatenation.
If one of the operands is a string, the interpreter will always choose concatenation rather than addition.
2 + '0' = '20'
typeof NaN returns string 'number'. Length of this string is 6.<br>
As a result, it turns out that on the left is the string '20', and on the right is the number 6, and between them is the - operator.
Minus is only a mathematical operator, so the interpreter sees that the operand on the left is not number, and automatically converts it to number.
20 - 6 = 14
Rate the difficulty of the question:
easyhard