Every value in the JavaScript language belongs to a certain data type. There are six types of JavaScript data. (ES6 has added a seventh symbol type value, which is not covered in this tutorial.)

-Number: integers and decimals (such as 1 and 3.14). -String: text (such as Hello World). -Boolean value (boolean): Two special values ​​representing authenticity, namely true (true) and false (false). -undefined: means "undefined" or does not exist, that is, because there is no definition currently, there is no value here for the time being. -null: indicates a null value, that is, the value here is null. -Object: A collection of various values.

Generally, the three types of numeric, string, and boolean are collectively called primitive type values, that is, they are the most basic data types and cannot be further subdivided. An object is called a complex type value, because an object is often a composite of multiple primitive types and can be seen as a container for storing various values. As for undefined and null, they are generally regarded as two special values.

Objects are the most complex data type and can be divided into three subtypes.

-Narrow object (object) -Array -Function

Objects and arrays in the narrow sense are two different ways of combining data. Unless otherwise stated, the "objects" in this tutorial refer to objects in the narrow sense. A function is actually a method of processing data. JavaScript treats it as a data type that can be assigned to variables. This brings great flexibility to programming and lays the foundation for JavaScript's "functional programming".

typeof operator

JavaScript has three ways to determine what type a value is.

-typeof operator -instanceof operator -Object.prototype.toString method

The instanceof operator and the Object.prototype.toString method will be introduced later. Here is the typeof operator.

The typeof operator can return the data type of a value.

Number, string, and boolean return number, string, and boolean respectively.

typeof 123; // "number"
typeof "123"; // "string"
typeof false; // "boolean"

The function returns function.

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

undefined returns undefined.

typeof undefined;
// "undefined"

Taking advantage of this, typeof can be used to check an undeclared variable without reporting an error.

// ReferenceError: v is not defined

typeof v;
// "undefined"

In the above code, the variable v is not declared with the var command, and an error will be reported if it is used directly. However, if it is placed after typeof, it will not report an error, but return undefined.

In actual programming, this feature is usually used in judgment statements.

// wrong way
if (v) {
  // ...
// ReferenceError: v is not defined

// correct writing
if (typeof v === "undefined") {
  // ...

Object returns object.

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

In the above code, the type of the empty array ([]) is also object, which means that in JavaScript, the array is essentially just a special kind of object. By the way, the instanceof operator can distinguish between arrays and objects. For a detailed explanation of the instanceof operator, please refer to the chapter "Object-Oriented Programming".

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

o instanceof Array; // false
a instanceof Array; // true

null returns object.

typeof null; // "object"

The type of null is object, which is caused by historical reasons. The first version of the JavaScript language in 1995 only designed five data types (objects, integers, floating-point numbers, strings, and boolean values). It did not consider null, and only regarded it as a special value of object . Later, null became independent as a separate data type. In order to be compatible with the previous code, typeof null returns object and cannot be changed.