The type of the data type

  • Basic data types: Undefined, Null, Boolean, Number, String, Symbol (new in ES6, representing unique values) and BigInt (new in ES10)
  • Reference data type: Object (an Object is essentially an unordered set of key-value pairs). Function, Array, Date, etc. JavaScript does not support any mechanism for creating custom types, and all values will ultimately be one of the eight data types mentioned above.

The storage mode of data types

  • Raw data type: stored directly in the stack. It occupies a small space and has a fixed size. It is frequently used data and therefore stored in the stack.
  • Reference data types: Stored in both stack and heap, occupying large space and of variable size. The reference data type stores a pointer on the stack to the starting address of the entity in the heap. When the interpreter looks for a reference value, it first retrieves its address in the stack and then retrieves the entity from the heap.

Data type judgment

Typeof returns a string representation of the data type

  • Typeof displays the correct type for primitive types except null
  • Typeof displays object for all objects except functions
  • Cannot distinguish between NULL and object/Object and array
  • Also, with NULL, many people think it is an object type, which is not true. Typeof NULL outputs objects, but this is a long-standing JS Bug. In the original version of JS, the 32-bit system was used. For the sake of performance, the type information of the variable was stored at a low level. The beginning of 000 represents an object, while null represents all zeros, so it was wrongly judged as object. Although the internal type determination code has changed, the Bug has persisted.
console.log(typeof 2);               // number
console.log(typeof true);            // boolean
console.log(typeof 'str');           // string
console.log(typeof Symbol('protein'))// symbol console.log(typeof []); // object [] the data typeof the array is interpreted in typeof as object console.log(typeoffunction() {}); //functionconsole.log(typeof {}); // object console.log(typeof undefined); // undefined console.log(typeof null); // object Null's data type is interpreted as object by TypeofCopy the code

Instanceof is used to check whether the constructor’s Prototype property appears on an instance’s prototype chain

  • Instanceof is the instance on the left and the constructor on the right. That is, determine whether left is an instance object of right. The internal mechanics are judged by the prototype chain
  • Instanceof can accurately determine the reference data types Array, Function, and Object, whereas primitive data types cannot be accurately determined by Instanceof because it is not itself an instance Object
console.log(2 instanceof Number);                    // false
console.log(new Number(2) instanceof Number)         // true
console.log(true instanceof Boolean);                // false 
console.log('str' instanceof String);                // false  
console.log(Symbol('protein') instanceof Symbol)     // falseSyntax not supported:"new Symbol()"
console.log([] instanceof Array);                    // true
console.log(function(){} instanceof Function);       // true
console.log({} instanceof Object);                   // true    
// console.log(undefined instanceof Undefined);
// console.log(null instanceof Null);
Copy the code

Constructor constructor

  • If I create an object and change its prototype, constructor becomes unreliable
console.log((2).constructor === Number);                // true
console.log((true).constructor === Boolean);            // true
console.log(('str').constructor === String);            // true
console.log(Symbol('protein').constructor === Symbol)   // true
console.log(([]).constructor === Array);                // true
console.log((function() {}).constructor === Function);  // true
console.log(({}).constructor === Object);               // true
console.log(new Date().constructor === Date)            // true
console.log(new RegExp().constructor === RegExp)        // true
console.log(new Error().constructor === Error)          // true// Change the prototype pointingfunction Fn() {}; Fn.prototype=new Array(); var f=new Fn(); console.log(f.constructor===Fn); //false
console.log(f.constructor===Array); // true
console.log(f instanceof Fn)        // true
console.log(f instanceof Array)     // true
Copy the code

Object.prototype.toString.call()

  • Use toString, the prototype method for Object, and call to change the reference to this
const a = Object.prototype.toString;
console.log(a.call(2));             // [object Number]
console.log(a.call(true));          // [object Boolean]
console.log(a.call('str'));         // [object String]
console.log(a.call(Symbol()))       // [object Symbol]
console.log(a.call([]));            // [object Array]
console.log(a.call(function() {})); // [object Function] console.log(a.call({})); // [object Object] console.log(a.call(undefined)); // [object Undefined] console.log(a.call(null)); // [object Null] console.log(a.call(new Date())) // [object Date] console.log(a.call(new Error())) // [object Error] console.log(a.call(new RegExp())) // [object RegExp]Copy the code

Determine the data type instance

  • Number — ! isNaN(parseFloat(arg))

Conversion of data types

Boolean Boolean value conversion

  • To convert to a Boolean value call the Boolean() method
  • !!!!! The operator can cast the value on the right to a Boolean, which is an easy way to convert a value to a Boolean
  • Change Boolean to true all values except undefined, null, false, 0, -0, NaN, ‘ ’empty string, including all objects.
The original value Transformation goal The results of
number Boolean All except 0, -0, and NaN are true
string Boolean True for all but the empty string ‘ ‘
Undefined, null, Boolean false
Reference types Boolean true
number string 6 = > ‘6’
Boolean, function, Symbol string ‘true’
An array of string [1, 2] => ‘1, 2’
object string ‘[ object, Object ]’
string number ‘6’ => 6, ‘p’ => NaN
An array of number [] => 0, [‘6’] => 6, all other NaN
null number 0
A non-array reference type number NaN
Symbol number Throw the wrong

Conversion of numbers to strings

  • Converting to numbers calls the Number(), parseInt(), and parseFloat() methods
  • To convert to a String, call the.toString() or String() methods

Four operators

  • Characteristics of the addition + operator
    • If one side of the operation is a string, the other side is converted to a string
    • If a party is not a string or number, it is converted to a number or string
console.log(1 + '1'); // 11
console.log(true + true); // 2
console.log(4 + [1, 2, 3]); // 41, 2, 3
console.log('a' + + 'b'); // aNaN because +'b'Is NaNCopy the code
  • Non-addition operators convert to numbers as long as one of the parties is not a number
console.log(2 * '1'); // 2
console.log(2 * []); // 0
console.log(2 * ['2']); // 4
console.log(2 * [1, 2]); // NaN
Copy the code

== and === operators

  • === determine whether the left and right types and values are equal
  • == If the left and right types are equal, a size comparison is performed. If the left and right types are not equal, a type conversion is performed
  • Checks whether null and undefined are compared. Returns true if so
  • Check whether the two types are string and number, if so, string is converted to number
1 = ='1' => 1 == 1 // true
Copy the code
  • Check if one of the parties is Boolean and if it is, it will convert it to number
'1'= =true= >'1' == 1 => 1 == 1 // true
Copy the code
  • To determine whether one is object and the other is string, number and symbol, object will be converted to the original type for judgment
'1'== { name: 'protein'} = >'1'= ='[ object, object ]' // false
Copy the code
  • Example: [] ==! []
[] = =! = = = > [] []false => [] == 0 => 0 == 0 // true
Copy the code