导航
×
   ❮   
HTML CSS JavaScript PHP Go ECMS

JavaScript 教程

JS 简介 JS 如何使用 JS 输出 JS 语法 JS 语句 JS 注释 JS 变量 JS 运算符 JS 条件语句 JS 循环 JS 字符串 JS 数字 JS 函数 JS 对象 JS 日期 JS 数组 JS 类型化数组 JS 集合 JS Map 映射 JS Math JS 正则表达式 JS 数据类型 JS 错误 JS 事件 JS 编程 JS 关键字参考 JS 保留关键字参考 JS 运算符参考 JS 运算符优先级 JS UTF-8 字符 JS UTF-8 符号 JS UTF-8 表情符号 JS 版本

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():

示例

// 创建一个数组
const fruits = ["apples", "bananas", "oranges"];
Array.isArray(fruits);
亲自试一试 »

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 car;
亲自试一试 »

对没有值的变量进行 typeof 操作是 undefined。该值也为undefined

示例

let car;
typeof car;
亲自试一试 »

任何变量都可以通过将值设置为 undefined 来清空。

类型也将变为 undefined

示例

let car = "Volvo";
car = undefined;
亲自试一试 »

空值

空值与 undefined 无关。

空字符串既有合法值,也有合法类型。

示例

let car = "";
typeof car;
亲自试一试 »

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 的区别

undefinednull 的值相等,但类型不同:

 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

亲自试一试 »

通过构造函数,您可以检查一个对象是否为数组:

示例

(myArray.constructor === Array);

亲自试一试 »

通过构造函数,您可以检查一个对象是否为Date:

示例

(myDate.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>
亲自试一试 »

freew3c.com 中文网是独立运营的中文开发者学习平台,与 freew3c.com 无关联。提供的内容仅用于学习和测试,不保证内容的正确性。


Copyright @2020-2026 京ICP备888888号-8