jirengu / frontend-interview

前端笔试面试题题库
1.29k stars 139 forks source link

JS运算符typeof 相关问题 #10

Open jirengu opened 7 years ago

jirengu commented 7 years ago

以下代码输出什么 ?why

    var str = new String("Hello");

    var result = typeof(str instanceof String);
    alert(result); //What is the output of the alert? 

    result = typeof typeof(str instanceof String);
    alert(result); //What is the output of the alert? 

    result = typeof typeof typeof(str instanceof String);
    alert(result); //What is the output of the alert?   
Panda-HJN commented 7 years ago

依次返回的是

//boolean
//string
//string

strString的一个实例,所以str instanceof String返回的是 true
true是一个布尔值,所以typeof true返回的是 "boolean"

typeof用于判断数据类型,以字符串的形式返回数据类型. "boolean"是个字符串,所以 typeof "boolean" 返回的是"string"

typeof "string" 返回的是 "string"

oceanpad commented 7 years ago
boolean
string
string

str is String, so str instanceof String is true(Boolean). Then typeof Boolean is Boolean, return 'boolean'. typeof's result is String, so typeof typeof ... all is string.