Home > Article > Web Front-end > Summary of the debate over "basic types" in JavaScript_javascript skills
One of the questions often asked in front-end interviews is "How many basic types of JavaScript are there?".
Some answers are "numbers, strings, Boolean", and some may add "Null, Undefined". And some people may think that "object" is also a basic type. What is it like?
The concept of so-called "primitive types" does not exist at all in ECMAScript (V3, V5). It just divides types into 6 types.
ECMAScript only mentions types, represented by types. In V3 and V5, searching for the keyword "primitive" in the entire document does not show "primitive type", but "primitive value" does exist, such as true/false/0-9, etc. This shows that the specification has no so-called "basic types" and "primitive types" at all. If you have to say that there is, then object is obviously the same, that is, there are 6 basic types.
Some books often use this concept, such as "JavaScript Advanced Programming" First Edition, Second Edition, and Third Edition.
The first edition mentioned that there are 5 primitive types, namely Undefined, Null, Boolean, Number, and String
The second and third editions came up with a simple data type, which is translated as a simple data type (also called a basic data type), and the object is called a complex data type.
Primitive types are also mentioned in Chapter 3 of another classic book "The Definitive Guide to JavaScript (6th Edition)"
First divided into primitive types (primitive types) and object types. Basic types are divided into numbers, strings, Boolean, and two special null and undefined.
Five years ago, there were very few books on JavaScript. These two books were two of them. They had a great influence and can be called classics. Therefore, there are many JSers affected by this. I think it is also in line with learning logic for the author to divide language types into basic types (metatypes) and object types (complex types). Complex types are composed of simple types. In addition, it is also influenced by other languages such as Java, which also divide types into basic types (8 types) and object types.
But there are no so-called primitive types in ECMAScript, just like it has no concept of classes (people with other language abilities still like to use the noun "class" in JavaScript). What is mentioned in it is type, which can be understood as basic types, but there should be 6 types. The basic type here is also not a basic type in Java, because it contains complex data types - objects.
Not all books believe that there are only 5 basic types in JavaScript (excluding objects). "The Essence of JavaScript Language and Programming Practice" believes that there are 6 types, as follows
It does not include null but adds function type. Oh, there are all kinds of weird answers. How should you answer this question next time someone asks you?