search

Home  >  Q&A  >  body text

javascript - Questions about typeof of js

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学习ing2720 days ago780

reply all(5)I'll reply

  • 天蓬老师

    天蓬老师2017-06-15 09:25:38

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

    reply
    0
  • 大家讲道理

    大家讲道理2017-06-15 09:25:38

    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

    reply
    0
  • 代言

    代言2017-06-15 09:25:38

    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

    reply
    0
  • 学习ing

    学习ing2017-06-15 09:25:38

    Nothing wrong

    Boolean('string') => true

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-06-15 09:25:38

    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

    reply
    0
  • Cancelreply