基本类型
基本类型有以下五种:
- Undefined
- Null
- Boolean
- Number
- String
typeof操作符
用于检测变量的数据类型
检测结果有以下6种: - “undefined” 未定义
- “boolean” 布尔类型
- “string” 字符串
- “number” 数字
- “object” 对象或null
- “function” 函数
example:1
2
3
4
5
6
7
8var tempString = "Hello World!";
var tempNumber = 123;
var tempBool = true;
var tempUndefined;
console.log(typeof tempString); //"string"
console.log(typeof tempNumber); //"number"
console.log(typeof tempBool); //"boolean"
console.log(typeof tempUndefined) //"undefined"
instanceof操作符
用于检测引用类型,判断给定变量是否是指定的引用类型,若使用其来检测基本数据类型都将返回false,因为其不是引用类型
example:1
2
3
4
5var person = {};
var persons = [];
console.log(person instanceof Object); //true
console.log(persons instanceof Array); //true
console.log(persons intanceof Object); //true,因为所有引用类型都是Object的实例