js判断某变量是不是字符串

var str = 'asdf';
var str1 = new String('asdf');
typeof str;//"string"
typeof str1;//"object"
Object.prototype.toString.cal(str); //"[object String]"
Object.prototype.toString.call(str1); //"[object String]"
综上所述,判断是否为字符串使用下边的最保险
Object.prototype.toString.call(str) === "[object String]"

你可能感兴趣的:(js判断某变量是不是字符串)