There are a total of 6 data types in JavaScript. It can be divided into two categories: simple data types and complex data types.
To clarify, the above statement should not be correct now, as ES6 has added data types. However, we are not currently involved in ES6.
Simple data types, also known as basic data types, have five types: Undefined, null, Boolean, Number, and String, with values corresponding to undefined, null, Boolean values true and false, numeric, and string values, respectively.
There is only one type of complex data, which is Object – Object. The objects in JS are a set of unordered key value pairs enclosed by curly braces {}. Object is very commonly used, powerful, and interesting, and for the language structure of JS, the meaning of Object is not ordinary. Let’s discuss these later in the article.
Next, let’s take a closer look at the six data types of JavaScript.
However, first, it is necessary to understand an operator – typeof. As the name suggests, the function of this operator is to return the data type of a variable. Using the typeof operator on a value always returns one of the following six strings: ‘undefined’, ‘boolean’, ‘number’, ‘string’, ‘object’, ‘function’. You must have discovered a problem, which is why these 6 strings do not correspond exactly to the 6 data types? Is there no ‘null’ Where did function come from? Please refer to the following test code:Note that using the typeof operator to return a unique value of null type is not a ‘null’, but an ‘object’. Why is this? This is because null is a unique value of the basic data type null, but at the same time, it is considered an empty object reference.
In JS, the relationship between Function and Object is subtle and complex, and it is necessary to have an understanding of prototype construction, prototype chains, and other knowledge before further understanding the relationship between the two. However, this does not affect our current understanding. From the perspective of data types, we can clearly believe that a Function is a special type of object. As for the return result of using the typeof operator on a Function, it is’ function ‘rather than’ object ‘, as mentioned in the book, simply because we need to know that it is a function rather than a general object.
After introducing the typeof operator, a practical tool, let’s take a closer look at the six data types in JS.