Stack memory

  • Stack: store variables; Automatically allocate memory space in memory.

  • Heap: holds complex objects; Memory space dynamically allocated in the memory may not be automatically released. After the memory space is used up, the object is manually set to NULL.

Primitive types are data stored on the stack as values.

Reference types are stored in heap memory, where each object has a reference address.

Second, data type

JavaScript data types include primitive types and reference types (object types).

Raw data type:

  • String String

  • The Number of numerical

  • Boolean Boolean value

  • Null Null values

  • Undefined Undefined

  • BigInt Indicates the number type

  • Symbol (ES6)

Reference data types:

  • Object

Type conversion

0.1 + 0.2 > 0.3

The reason: JS USES a double-precision version, this version is accuracy problems, all computer information is converted into binary for storage, then the binary representation of 0.1 is an infinite decimals, the version of the JS USES a floating-point standard need to intercept the binary infinite loop, resulting in lost the accuracy, 0.1 is no longer 0.1, 0.1 is now 0.100… 001, 0.2 becomes 0.200… 002. So the sum is greater than 0.3.

1. String conversion

  • toString()

  • String()

  • Data and +””/ “concatenated will be converted to a string;

    let a=111;

    a.toString() //”111″

    String(a) //”111″

    a+” //”111″

2, numeric type conversion:

2.1, the Number ()

  • String to numeric value:

    // If it is a purely numeric string, it is directly converted to a number;

    Number(‘123’); / / 123

    // If there is any non-numeric content in the string, convert to NaN;

    Number(“123aaa”); //NaN

    // If the string is an empty string or a string full of Spaces, it is converted to 0;

    Number(”); / / 0

  • Boolean conversion value:

    Number(true); / / 1

    Number(false); / / 0

  • Null converts to a value:

    Number(null) //0

  • Undefined to numeric:

    Number(undefined) //NaN

  • Convert an object to a number: First convert an object to a string, then to a number

2.2, parseInt(), parseFloat()

ParseInt () extracts an integer from a string: if the first digit is not a significant number, nothing is extracted and NaN

parseInt('12aaa') //12
parseInt('aaa111aaa') //NaN
Copy the code

ParseFloat extracts decimals in strings

12.5.2 parseFloat (' ') / / 12.5 parseFloat (' a12.5.2 ') / / NaNCopy the code

IsNaN determines the number type:

Returns false if the current type is numeric, true otherwise. If the string is currently passed in, isNaN() converts the string to a Number using the Number() method, and then determines whether it is currently a string type.

3, Boolean type conversion: Boolean()

All values are converted to true except for the special values’ ‘, ‘ ‘NAN’, ‘null’, ‘false’, and ‘0’, which are Boolean false.

Boolean("11") //true
Boolean("")//false
Boolean(undefined)//false
Boolean(null)//false
Boolean(NaN) //false
Copy the code

Four, the detection of data type

  • typeof
  • instance of
  • constructor 
  • Object.prototype.toString.call()

4.1, typeof

The result of the check is returned as a string, typically used to determine whether a variable is empty or of what type. With the exception of null and Object, all data types may return the correct type.

Typeof '123' //'string' typeof 123 // "number" typeof NaN // "number" typeof Function //' Function 'typeof null // 'Object' typeof [] // 'Object' typeof {} // 'Object'Copy the code

Typeof NULL equals Object?

The storage of different objects in the underlying principle is represented by binary. In javaScript, if the first three digits of the binary are all 0, the system will judge the Object type. The storage binary of NULL is 000, which is also the first three digits. Therefore, the system identifies NULL as Object.

4.2, the instance of

It is used to determine whether an object is an instance of another object by determining whether the prototype type of the construct type can be found in the prototype chain of the object.

function Foo(){} var f1 = new Foo(); console.log(f1 instanceof Foo); // true f1.__proto__=Foo.prototypeCopy the code

4.3, the constructor

In addition to reference types, you can use the xx.constructor.name constructor.

Function Foo(){} console.log(foo.constructor.name) // "function"Copy the code

4.4, the Object. The prototype. ToString. Call ()

A way of the best basic types of test Object. The prototype. ToString. Call (); It can distinguish between null, string, Boolean, number, undefined, array, function, object, date, and Math data types.

The disadvantage is that it cannot distinguish whether XX is an instance of YY.

/ / determine the basic type Object. The prototype. ToString. Call (null); // "[object Null]" Object.prototype.toString.call(undefined); / / "[object Undefined]" is the object. The prototype. ToString. Call (" ABC "); // "[object String]" Object.prototype.toString.call(123); // "[object Number]" Object.prototype.toString.call(true); Function fn(){console.log(" xx "); function fn(){console.log(" xx "); } var date = new Date(); Var arr = [1, 2, 3]; var reg = /[a]at/g; Object.prototype.toString.call(fn); // "[object Function]" Object.prototype.toString.call(date); // "[object Date]" Object.prototype.toString.call(arr); // "[object Array]" Object.prototype.toString.call(reg); // "[object RegExp]"Copy the code