> For the complete documentation index, see [llms.txt](https://zg-zhang.gitbook.io/note/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://zg-zhang.gitbook.io/note/javascript/es5/2-0/2-1.md).

# 数据类型概述

## 简介

> JavaScript 语言的每一个值，都属于某一种数据类型。JavaScript 的数据类型，共有6种。（ES6 新增了第七种 Symbol 类型的值）

* 数值 number：整数和小数
* 字符串 string：文本
* 布尔值 boolean：true 和 false
* undefined：表示未定义或不存在，
* null：空值，此处的值为空
* 对象 object：各种值组成的集合

数值、字符串、布尔值合成为原始类型 primitive type 的值。即他们是最基本的数据类型，不能再细分了

对象则称为合成类型 complex type 的值，因为一个对象往往是多个原始类型的值的合成，可以看作是一个存放各种值的容器

至于 undefined 和 null 一般将它们看成两个特殊的值

对象是最复杂的数据类型，又可以分成三个子类型：

* 侠义的对象 object
* 数值 array
* 函数 function

## typeof 运算符

> JavaScript 有三种方法，可以确定一个值到底是什么类型

* typeof 运算符
* instanceof 运算符
* Object.prototype.toString 方法

这里是介绍 typeof 运算符

```javascript
typeof 123 // "number"
typeof '123' // "string"
typeof false // "boolean"

function f() {}
typeof f // "function"

typeof undefined // "undefined"

typeof window // "object"
typeof {} // "object"
typeof [] // "object"

typeof null // "object"
```

null 的类型是 object 是由于历史原因造成的

利用 typeof undefined 返回 "undefined" 这一点，可以用来检查一个没有声明的变量，而不报错。 实际编程中，这个特点通常用在判断语句

```javascript
v
// ReferenceError: v is not defined

typeof v
// "undefined"

// 错误的写法
if (v) {
  //...
}
// ReferenceError：v is not defined

// 正确的写法
if (typeof v === "undefined") {
  //...
}
```

空数组 \[] 的类型也是 object，这表明在 JavaScript 内部，数组本质上只是一种特殊的对象。 instanceof 运算符可以区分数组和对象

```javascript
var o = {};
var a = [];

o instanceof Array // false
a instanceof Array // true
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://zg-zhang.gitbook.io/note/javascript/es5/2-0/2-1.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
