JavaScript:数据类型

查看变量数据类型:
typeof操作符,它总是返回一个字符串

typeof 123; // 'number'
typeof NaN; // 'number'
typeof 'str'; // 'string'
typeof true; // 'boolean'
typeof undefined; // 'undefined'
typeof Math.abs; // 'function'
typeof null; // 'object'
typeof []; // 'object'
typeof {}; // 'object'

typeofnullarray类型都返回'object'
判断Array要使用Array.isArray(arr)
判断null请使用myVar === null


判断某个全局变量是否存在用typeof window.myVar === 'undefined'
函数内部判断某个变量是否存在用typeof myVar === 'undefined'

nullundefined没有toString()方法。

number对象直接调用toString()报SyntaxError:

123.toString(); // SyntaxError

遇到这种情况,需特殊处理一下:

123..toString(); // '123', 注意是两个点
(123).toString(); // '123'
posted @ 2019-01-09 17:42  xuejianbest  阅读(116)  评论(0编辑  收藏  举报