Image source:Pixabay.com/zh/photos/m…

JS two big data types

Officially defined names:

  • Primitive value type
  • Object data type

Primitive value types commonly known as simple data types, basic data types Object data types commonly known as complex data types, reference data types

Primitive value type

  • Number
  • String
  • Boolean
  • null
  • undefined
  • Symbol
  • bigint

The Number type

  • Members are numbers, NaN, Infinity, -infinity
  • NaN: The rate is of type Number, but not a valid Number
  • Infinity / – Infinity representative is infinite | minus Infinity

Number is special

  • We shouldn’t compare NaN to a number when we compare a significant number
/* NaN does not equal NaN */
console.log(NaN= = =NaN); //=> false

/* NaN does not equal any number */
console.log(NaN= = =1); //=> false
console.log(NaN= = =Infinity) // false
Copy the code
  • We should use isNaN when we want to see if a number is a significant number
/* isNaN method is a built-in JS function */
isNaN(NaN) //=> true
isNaN(1) //=> false
isNaN(Infinity) //=> false
Copy the code

Summary of the Number type

  • NaN does not equal NaN, so we should use isNaN instead of comparing equality to determine whether it is a significant number
  • IsNaN returns true if it is not a significant number, false if it is a significant number

Type String

  • Anything enclosed by single, double, or backquotes is called a string
  • Backquoted strings also called template strings are ES6 syntax
/* Single quotes' */
var str1 = 'This is a string.';

/* Double quotes "" */
var str2 = "This is a string.";

/* backquotes' '*/
var str3 = 'This is a string';
Copy the code

Pay attention to

  • Double quotes cannot enclose double quotes, single quotes cannot enclose single quotes, and backquotes cannot enclose backquotes. But they can wrap each other
  • If you want to concatenate two characters, use the hyphen (+).
/* concatenates strings */
var str1 = 'connection' + 'characters';

/* symbols wrap each other */
var str2 = 'See' love 'see' meaning ';
Copy the code

String summary

  • No matter what the content is, as long as the single quote, double quote, backquote is wrapped around a string, right
  • The same symbol is not allowed to be wrapped

Boolean type

  • Boolean has only true and false values
  • False only if a value is an empty string, an empty array, the digits zero, null, or undefined

Null and undefined

  • Null: indicates that this result is expected
  • Undefined: this result is unexpected
  • Both null and undefined mean empty

Example:

/* The parent node that gets the page's HTML(root) tag is expected */
var html = document.documentElement.parentNode;
console.log(html) //=> the result is null because the syntax knows that the root tag has no parent node, as expected

/* Get a nonexistent property of the object unexpected */
var obj = { name: 'obj' };
console.log(obj['age']); //=> undefined, because the syntax does not know that obj has the age property when it goes to fetch it, it is not unexpected, so undefined
Copy the code

Summary of null and undefined

Undefined when trying to get a property that is not known and does not exist and null when trying to get a property that is known but is empty

Symbol type

  • ES6 syntax added
  • A value declared with Symbol is unique and not enumerable

Example:

/* Declare that the two symbols are equal */
console.log(Symbol('A') = = =Symbol('A'));  // The result is false because they are identical, but both values are unique, so false

/* Gets the attribute */ in the object with the Symbol value as the key name
var sy = Symbol('A');
var obj = {
    name: 'obj',
    [sy]: 'symbolA'[Symbol('B')]: 'symbolB'
};
console.log(obj[Symbol('B')]); Obj => Symbol('B'); obj => Symbol('B')
console.log(obj[sy]); //=> symbolA, which refers to the same variable as obj so it gets the value

// Use object.keys to get enumerable properties
console.log(Object.keys(obj)); //=> ['name']

/ / need to use the Object. GetOwnPropertySymbols this method can only obtain symbol value
consoe.log(getOwnPropertySymbols(obj)); //=>[Symbol(A), Symbol(B)]


Copy the code

Symbol type summary

  • Symbol(‘A’) just uses ‘A’ as an identifier, but its value is still unique
  • Symbol value cannot be enumerated, need to use the Object. The getOwnPropertySymbols to obtain
  • If you want to get the same result, you need to use variables to hold the value temporarily. Otherwise you can’t get it

BigInt type

  • ES6 syntax added
  • BigInt ensures accuracy when working with very large numbers
  • A number followed by an n indicates that the number is of type BigInt

/* To BigInt */
console.log(BigInt(100)); //=> 100n

/* BinInt converts the data type to Number */
console.log(Number(9123452334564571n.toString())); / / = > 9123452334564571
Copy the code

BigInt type summary

  • A number followed by n is of type BinInt
  • When the operation exceeds the safe number, use the BigInt type, otherwise the accuracy will be incorrect
  • BitInt reverses the Number type by converting it to a string and then to a Number

Object data type

  • Standard common object
  • Standard special object
  • Non-standard special objects
  • Callable/executable object

Standard common object

  • Consists of 0 to multiple key/value pairs => {key: value}
  • Keys can only be strings and symbols

Example:

/* Define a normal standard object */
var name = 'obj';
var obj = { name: 'obj' };

console.log(obj[name]); // =>undefined 'obj' ['obj']
console.log(obj['name']); //=> 'obj'
Copy the code

Standard special object

  • Built-in classes in JS
  • Array, date, error

Example:

/* Built-in date object */
console.dir(Date); //=> Print details using dir instead of log
Copy the code

Non-standard special objects

  • Based on the primitive value type, which is based on the instance of the constructor new
/* new a nonstandard special object */
console.log(new Number(1)); //=> Number {1}
Copy the code

Executable/callable object

  • Function object
  • A function is a special object

At the end

Dear reader, thank you for seeing it through. I hope you can learn some knowledge in my article. If there are any mistakes in this article, please feel free to correct them in the comments section or in my private message. Thank you!