JavaScript Advanced Programming Edition 4 and MDN are used as references.

The data type

JavaScript is a weakly typed or dynamic language. The type is automatically determined during program execution. This also means that you can use the same variable to hold different types of data.

let a = true;
a = 1;
a = 'string'
Copy the code

The data type of little Red Book is inconsistent with THAT of MDN. Considering the timeliness of books, MDN is used as the standard record.

MDN–JavaScript data types and data structures

The latest ECMAScript standard defines nine data types:

  • Six primitive types:
    • undefiend typeof instance === "undefined"
    • Boolean typeof instance === "boolean"
    • Number typeof instance === "number"
    • String typeof instance === "string"
    • BigInt typeof instance === "bigint"
    • Symbol typeof instance === "symbol"
  • null typeof instance === "object"Null is considered a pointer to an empty object, so object is returned
  • Object typeof instance === "object"Any particular non-data structure type of a constructed instance is also used as a data structure: New Object, New Array, New Map, New Set, New WeakMap, New WeakSet, New Date, and almost anything created with New keyword.
  • Funtion is not a data structure, although typeof operations result in:typeof instance === "function". This result is a special abbreviation for Function, although each Function constructor is derived from the Object constructor.

The value returned by the Typeof operator may not be what we want. The instanceof operator can be used to determine structure types derived from Object, array.isarray (), isNaN(), and so on.

Type Supplement

Number:

According to the ECMAScript standard, there is only one numeric type in JavaScript: values (-(2^ 53-1) to 2^ 53-1) in the double precision 64-bit binary format based on the IEEE 754 standard. In addition to the general number types, +Infinity, -infinity, and NaN are also considered number. In ECMAScript 6, You can also use the number.isSafeINTEGER () method as well as number.max_safe_INTEGER and number.min_safe_INTEGER to check whether the value is in the range of double precision floating point values. Outside of this range, numbers in JavaScript are no longer safe, i.e. only second Mathematical Interger can be correctly represented in JavaScript number types.

Floating-point numbers have twice as much memory as integers, so ECMAScript will always try to convert them to integers: let a = 1.0 is treated as an integer.

Floating-point calculations can be inaccurate. 0.1 + 0.2! = = 0.3

NaN: NaN == NaN and NaN === NaN both get false, and we usually use isNaN(). IsNaN (xx) attempts to convert xx to Number and returns true if it fails.

  • isNaN(window) => true
  • isNaN(true) => falseTrue is converted to 1 of type Number, false to 0
  • isNaN('10') => false
  • isNaN('abc') => true
  • isNaN(10) => false
  • isNaN(undefiend) => true

BigInt

BigInt is created by appending n to the end of the integer or by calling the constructor.

const x = 2n天安门事件53n;
9007199254740992n
const y = x + 1n;
9007199254740993n
Copy the code

Boolean

We usually use if statements to test conditions. For shorthand, if conditions are not Boolean:

const a = {};
const b = 1;
const c = 'asfa';
if(a && b && c) ...
Copy the code

If the condition in if is not Boolean, Boolean() is cast to Boolean type. Here are some of the transformation relationships:

The data type The results of true The results of false
Boolean true false
String Non-empty string An empty string
Number Non-zero value 0
Object Arbitrary objects and extended data structures for objects null
undefiend undefiend

Type declaration

There are three types of declaration operators: var, let, and const. You can also write directly without using the operator.

No operators are used

Gg = 1 Global variables are created by default and are not recommended

var

A product of a bygone era, but it’s still important to know that in another 10 years someone will still be maintaining var code.

There is a variable boost, and the scope penetrates if, for, switch, and so on. No further use is recommended.

let

Variables declared have block-level scope, and the same scope cannot be declared again. Cannot re-declare var variables.

Note that using let to declare variables is similar to an IIFE effect:

// global scope
let a = 1
var b = 1
window.a === undefined
window.b === 1
// Using let is similar to
(function(){
  var a = 1; }) ();// Common questions about block-level scope
for(var i = 0; i < 5; i++) {
  setTimeout(() = > {
    console.log(i); / / 5 5
  }, 0)}for(let i = 0; i < 5; i++) {
  setTimeout(() = > {
    console.log(i); / / 01234
  }, 0)}Copy the code

const

The memory address that a variable points to cannot be modified. If the Object type is used, you can perform other operations as long as the memory address is not changed.

const a = 1;
a = 2; // VM6330:1 Uncaught TypeError: Assignment to constant variable.
a = '1'; // VM6330:1 Uncaught TypeError: Assignment to constant variable.

const b = [];
a.push(1); // Normal execution

const c = {name: 1};
c.name = 2; // Normal execution
Copy the code