Home > Web Front-end > JS Tutorial > body text

There are several data types in javascript

王林
Release: 2021-11-02 16:51:57
Original
3426 people have browsed it

There are 8 data types in JavaScript, namely number, string, boolean, null, undefined, symbol, bigint and object.

There are several data types in javascript

#The operating environment of this article: windows10 system, javascript 1.8.5, thinkpad t480 computer.

First of all, data types in js are divided into primitive data types and reference data types

  • Primitive data types

    • # #number

    • string

    • boolean

    • null

    • undefined

    • symbol (ES6)

    • bigint (ES10)

  • Reference data type

    • ##object
    • ##Include function/array/object
    in the reference data type object
Seeing here we can find a total of 8 data types.

New in ES6: Symbol

This data type is mainly used to create a unique identity. No nonsense, the above code:

let obj = {};
obj.a = 1;
let a = Symbol();
obj[a] = 2; // 此时obj内部的a到底是1还是2呢?我们可以打印一下obj
 
obj {
 a: 1
 Symbol(): 2
 __proto__: Object
} // 此时我们可以发现定义的变量a似乎只是一种标志,并没有具体的属性名
Copy after login

Symbol can be passed during execution A string, for example:

let obj = {};
obj.a = 1;
let a = Symbol('a');
obj[a] = 2; 
 
obj {
 a: 1
 Symbol(a): 2 // 有字符串的Symbol更能区分每个独一无二的标识
 __proto__: Object
}
Copy after login

New in ES10: Bigint

We all know that there are precision issues with extreme values ​​in javascript, for example:

let a = Number.MAX_SAFE_INTEGER; // 9007199254740991 最大安全整数
console.log(a + 1); // 9007199254740992
console.log(a + 2); // 9007199254740992
console.log(a + 1 === a + 2); // true
// 此时我们可以发现 a + 1 与 a + 2 计算出来的数值是一样的,虽然可以正常计算,但是已经失去了计算的价值
Copy after login

At this time We can find that the calculated values ​​​​of a 1 and a 2 are the same. Although they can be calculated normally, the value of calculation has been lost

BigInt is a built-in object, which provides a representation that is greater than the maximum safety Methods other than integers, bigint are usually used to calculate values ​​other than the largest safe integer:

BigInt(1) === BigInt('1') === 1n
Copy after login

Summary:

JavaScript has a total of 8 data types, of which 7 are primitive data types (basic data type), 1 reference value type

Recommended learning:

javascript video tutorial

The above is the detailed content of There are several data types in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!