It's 2021! How many data types does JavaScript have?


I have read some articles, and there are different opinions about the data types of JavaScript. First of all, from a general perspective, some talk about basic data types and reference data types; some talk about primitive data types and object data types, and so on.

Combine the MDN document and some online articles to organize it here.

First conclusion: eight kinds, namely: string, number, boolean, undefined, null, symbol (ES6), bigint (ES10) and object

1. Stack and data type in JS

1. Start with the stack in JavaScript

First we need to know:

  • JavaScript is an interpreted language
  • JavaScript code needs to install a tool (JS engine) on the machine (node ​​or browser) to execute.

However, JS engines (such as chrome v8, etc.) are mostly "applications" developed by C++. There is no strict distinction between stack memory and heap memory in JavaScript. How to do it depends on the implementation of different engines.

We need to refer to some compiled languages ​​to see the JS stack.

  • Stack (stack): The stack will automatically allocate memory space, will automatically release, store basic types.
  • Heap: Dynamically allocated memory. The size is variable and will not be automatically released. It stores reference types. The storage address in the stack memory points to an object in the heap memory.

When querying a variable of a reference type, first read the memory address from the stack, and then find the value in the heap by the address.
Insert picture description here
Insert picture description here

2. Look at the data type from the stack

In summary, we roughly divide the data types in JS into two types: basic data types and reference data types.

There are currently eight data types for JS , namely:

  • string、number、boolean、undefined、null、symbol(ES6)、bigint(ES10)
  • object

In this way, we start from the JS stack and further understand the data types of JavaScript, which will help us understand the value and address, depth copy and so on.

Note: function function is actually an object, in the heap memory. The object is again through the new constructor...This part can be used to learn the prototype and the prototype chain, which is helpful for further understanding.

2. Unchangeable basic data types

Written in MDN

Basic types (basic values, basic data types) are data that is neither an object nor a method. In JavaScript, there are 7 basic types: string, numbe, boolean, null, undefined, bigint, symbol.

In most cases, the basic types directly represent the lowest level language implementation.

The values ​​of all basic types are immutable.

The basic type value can be replaced, but cannot be changed, and the assignment behavior can give the basic type a new value instead of changing it.

In the function, JavaScript will make a copy of the passed-in parameter (the value of the basic type) and create a local copy. Any operation in the function will not affect the original value. What we operate is just a copy of it.

Then the method of calling the string will not affect the previous string; the method of calling the array will partially affect the previous array.

Three, basic types, packaging objects, prototypes in JS

In addition nulland undefinedbeyond, all the basic type has its corresponding wrapper object:

  • String It is the basic type of string.
  • NumberIt is the basic type of value.
  • BigInt It is the basic type of large integer.
  • BooleanIt is a Boolean basic type.
  • Symbol It is the basic type of literal.

There are basic types of methods on the prototype of the packaging object, for example, there are split, substring, etc. methods in String.prototype.

Our basic types of __proto__property will point to the prototype object corresponding to the package , then we could be seen as examples of basic types, combined with the knowledge of the prototype and prototype chain.

In this way, basic data types can be found through the prototype chain when these methods are called.

const str = "abc";
const num = 123;
str.__proto__ === String.prototype // true
num.__proto__ === Number.prototype // true

4. What results can the typeof operator return?

typeof The operator returns a string indicating the type of the unevaluated operand.

Types of result
Undefined "undefined"
Null "object"
Boolean "boolean"
Number "number"
BigInt "bigint"
String "string"
Symbol "symbol"
Function "function"
Any other object "object"

Special points:

  • typeof Object: The result is "function", because Object is a constructor, and the object can be constructed through the new operator.

  • typeof null: The result is "object", null is a special type with only one value. Represents an empty object reference.

    Why typeof null is "object"? This has been the case since the birth of JavaScript

    In the original implementation of JavaScript, the value in JavaScript was represented by a label representing the type and the actual data value. The type label of the object is 0. Since nullit represents null pointer (most internet is 0x00), and therefore, the null tag type is 0, typeof nulland thus returns "object".

    There was a proposal to fix ECMAScript (by opt-in), but the proposal was rejected typeof null === 'null'.

In summary, the types that the typeof operator can return:

  • string number boolean undefined symbol bigint
  • object 、(typeof null === ‘object’)
  • function

5. == and === related summary

== operator, implicit type conversion occurs

// 以下结果都是true
100 == '100'
0 == ''
0 == false
1 == true
false == ''
null == undefined

Usually we use === everywhere, and use == when judging null or undefined

const obj = {
    
     foo: "foo" }
if (obj.a == null) {
    
    }
// 相当于
if (obj.a === null || obj.a === undefined) {
    
    }

It is similar in ==, if, and logical judgment. Learn about implicit type conversion rules on the Internet.

Detailed JS implicit type conversion

I looked at the interview questions on the Internet, especially the bizarre js invisible conversion interview questions, and I felt very outrageous and meaningless.

Guess you like

Origin blog.csdn.net/weixin_43792004/article/details/112975712