JavaScript typeof 运算符
typeof 运算符
typeof 运算符返回 JavaScript 变量的数据类型。
基本数据类型
在 JavaScript 中,基本数据类型是指不包含任何属性或方法的单个值。
JavaScript 共有 7 种基本数据类型:
- string
- number
- boolean
- bigint
- symbol
- null
- undefined
typeof运算符返回变量或表达式的类型。
示例
typeof "John" // Returns string
typeof ("John"+"Doe")// Returns string
typeof 3.14 // Returns number
typeof 33 // Returns number
typeof (33 + 66) // Returns number
typeof true // Returns boolean
typeof false // Returns boolean
typeof 1234n // Returns bigint
typeof Symbol() // Returns symbol
typeof x // Returns undefined
亲自试一试 »
typeof null // Returns object亲自试一试 »注意:
在 JavaScript 中,null 是一个原始值。但是,typeof 返回"object"。
这是 JavaScript 中一个众所周知的 bug,并且有历史原因。
复杂数据类型
复杂数据类型可以存储多个值和/或不同类型的数据。
JavaScript 有一种复杂数据类型:
- 对象
所有其他复杂类型,例如数组、函数、集合和映射,实际上都是不同类型的对象。
typeof 运算符仅返回两种类型:
- 对象
- 函数
示例
typeof {name:'John'} // 返回对象
typeof [1,2,3,4] // 返回对象
typeof new Map() // 返回对象
typeof new Set() // 返回对象
typeof function (){} // Returns function
亲自试一试 »注意:
typeof 运算符对所有类型的对象都返回 object:
- 对象
- 数组
- 集合
- 映射
您不能使用 typeof 来判断 JavaScript 对象是数组还是日期。
如何识别数组
如何判断一个变量是否为数组?
ECMAScript 5 (2009) 定义了一个新的方法:Array.isArray():
instanceof 运算符
instanceof 运算符返回 true,表示一个对象是指定对象类型的实例:
示例
// 创建一个日期
const time = new Date();
(time instanceof Date);亲自试一试 »
// 创建一个数组
const fruits = ["apples", "bananas", "oranges"];
(fruits instanceof Array);亲自试一试 »
// 创建一个映射
const fruits = new Map([
["apples", 500],
["bananas", 300],
["oranges", 200]
]);
(fruits instanceof Map);亲自试一试 »
// 创建一个集合
const fruits = new Set(["apples", "bananas", "oranges"]);
(fruits instanceof Set);亲自试一试 »未定义变量
typeof 未定义变量的值为 undefined。
对没有值的变量进行 typeof 操作是 undefined。该值也为undefined。
任何变量都可以通过将值设置为 undefined 来清空。
类型也将变为 undefined。
空值
空值与 undefined 无关。
空字符串既有合法值,也有合法类型。
Null
在 JavaScript 中,null 表示"无"。它应该代表一个不存在的事物。
遗憾的是,在 JavaScript 中,null 的数据类型是一个对象。
你可以通过将其设置为 null 来清空对象:
示例
// 创建一个对象
let person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
person = null;
// 现在值为空,但类型仍然是对象
亲自试一试 »您还可以通过将对象设置为 undefined 来清空对象:
示例
let person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
person = undefined;
// 现在值和类型都未定义。
亲自试一试 »Undefined 和 Null 的区别
undefined 和 null 的值相等,但类型不同:
typeof undefined // undefined
typeof null // object
null === undefined // false
null == undefined // true亲自试一试 »constructor 属性
constructor 属性返回所有 JavaScript 变量的构造函数。
示例
// Returns function Object() {[native code]}:
{name:'John',age:34}.constructor
// Returns function Array() {[native code]}:
[1,2,3,4].constructor
// Returns function Date() {[native code]}:
new Date().constructor
// Returns function Set() {[native code]}:
new Set().constructor
// Returns function Map() {[native code]}:
new Map().constructor
// Returns function Function() {[native code]}:
function () {}.constructor
亲自试一试 »通过构造函数,您可以检查一个对象是否为数组:
通过构造函数,您可以检查一个对象是否为Date:
全部测验
typeof "John" // Returns "string"
typeof ("John"+"Doe") // Returns "string"
typeof 3.14 // Returns "number"
typeof (33 + 66) // Returns "number"
typeof NaN // Returns "number"
typeof 1234n // Returns "bigint"
typeof true // Returns "boolean"
typeof false // Returns "boolean"
typeof {name:'John'} // Returns "object"
typeof [1,2,3,4] // Returns "object"
typeof {} // Returns "object"
typeof [] // Returns "object"
typeof new Object() // Returns "object"
typeof new Array() // Returns "object"
typeof new Date() // Returns "object"
typeof new Set() // Returns "object"
typeof new Map() // Returns "object"
typeof function () {} // Returns "function"
typeof x // Returns "undefined"
typeof null // Returns "object"亲自试一试 »注意:
NaN(非数字)的数据类型为数字!
void 运算符
void 运算符会计算表达式的值并返回 undefined。此运算符通常用于获取 undefined 原始值,例如使用"void(0)"(在计算表达式时,如果不需要返回值,则此运算符非常有用)。
示例
<a href="javascript:void(0);">
Useless link
</a>
<a href="javascript:void(document.body.style.backgroundColor='red');">
Click me to change the background color of body to red
</a>
亲自试一试 »