Consider the following code:
console.log(typeof ('6'/'2')) // => "number"
Now if I remove the parenthesis after typeof
console.log(typeof '6' / '2') // => NaN
which shows a different output. I'm wondering how JavaScript engine is working here.
CodePudding user response:
typeof '6' returns 'string'.
When you remove the parenthesis, the condition becomes 'string' / '2', which is NaN.
If you keep the parenthesis, the number division is done first, then typeof is called on the number.
CodePudding user response:
Because the code in parentheses executes first, So in the first statement will output 2, and the typeof 2, is number
But in the second statement, it will get typeof '6' first, which equal to string, then will do a dividing operation, which will equal to NaN
CodePudding user response:
typeof is an unary operator.
A unary operation is an operation with only one operand.
typeof '6' is processing the string '6' and then divided by '2' which results in NaN ('string' / '6'). While ('6'/'2') is calculated and casted to a number.
