var abc;alert(typeof abc);// undefinedvar abc =true;alert(typeof abc);// booleanvar abc =123;alert(typeof abc);// numbervar s ='abc';alert(typeof s);// stringvarabc=function(){};alert(typeof abc);// functionvar abc ={};//[] , nullalert(typeof abc);// objectvar s =Symbol();alert(typeof s);// symbol
typeof的局限性
typeof在判断对象类型时有局限性。
let d =newDate();let a =[];let n =null;let r =/\d+/;
consolel.log(typeof d);// object
consolel.log(typeof a);// object
consolel.log(typeof n);// object
consolel.log(typeof r);// object
二.obj instanceof constructor
判断变量是否是给定类的实例。
let d =newDate();let a =[];let n =null;let r =/\d+/;
consolel.log(d instanceofDate);// true
consolel.log(a instanceofArray);// true
consolel.log(n instanceofObject);// false
consolel.log(r instanceofRegExp);// true