There are 8 data types in JavaScript, namely number, string, boolean, null, undefined, symbol, bigint and object.
#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
let obj = {}; obj.a = 1; let a = Symbol(); obj[a] = 2; // 此时obj内部的a到底是1还是2呢?我们可以打印一下obj obj { a: 1 Symbol(): 2 __proto__: Object } // 此时我们可以发现定义的变量a似乎只是一种标志,并没有具体的属性名
let obj = {}; obj.a = 1; let a = Symbol('a'); obj[a] = 2; obj { a: 1 Symbol(a): 2 // 有字符串的Symbol更能区分每个独一无二的标识 __proto__: Object }
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 计算出来的数值是一样的,虽然可以正常计算,但是已经失去了计算的价值
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
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!