javascript - Questions about typeof of js
学习ing
学习ing 2017-06-15 09:23:38
0
5
776

The return result of typeof I saw today is actually an integer. And why the results of the results in the picture are 1.

学习ing
学习ing

reply all(5)
Peter_Zhu

1. Because typeof has a higher priority
2. Or because the addition operator has a higher priority

大家讲道理
var a=true;
console.log(a);//返回true
console.log(typeof a);//返回boolean
console.log(typeof(typeof a));//返回"string"

Essentially typeof a ? 1 : 2 can be written like this:

"boolean" ? 1:2

And this ternary expression calls Boolean() to convert the string type, that is:
Boolean("boolean") ====>true
So the return is 1.
You can try assigning a to false, and the return value will still be 1:

var a=false;
typeof a ? 1 : 2
代言

This statement is compiled like this
(typeof a)? 1 : 2
When it was compiled, it was changed to this form, and this is the result. Ternary operator

学习ing

Nothing wrong

Boolean('string') => true
伊谢尔伦

Taking your example, the
ternary operator, when typeof a is true, returns 1, and when
typeof a is false, it returns 2.
The first 100 + a; has been type converted, so 100 + a = 101,
so results also returns 1

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template