Reference article:

  • Js Deep copy vs shallow copy
  • JavaScript data type and data structure — MDN
  • InterviewMap -yck

The data type

JS is divided into seven types of data, the seven built-in types are divided into two types: six basic types and Object

1. Basic types

The basic types are Undefined, Boolean, String, Number, Null, and Symbol(new in ECMAScript 6).

Put on the stack

The basic type is stored in the stack memory, the data size is determined, the memory space size can be allocated, stored by value, so it can be directly accessed

Value is immutable

In javascript, there is a fundamental difference between raw values (undefined, NULL, boolds, numbers, and strings) and objects (including arrays and functions). Raw values are immutable: no method can change (or “mutate”) a raw value. This is obvious for numbers and Booleans — changing the value of a number itself makes no sense, but it is less obvious for strings, which look like arrays of characters, and we expect to be able to fake the characters in the string by specifying an index. In fact, javascript forbids this. All methods in a string that appear to return a modified string actually return a new string value.

The value is immutable, but can be reassigned, for example:

var a = "123";

console.log(a[1] ='0'); / / 0

console.log(a); / / 123

a = "234";

console.log(a); / / 234
Copy the code

A comparison of basic types is a comparison of values

As long as the values are equal, it is recommended to use === for comparison. For example:

var a = 1;
var b = 1;
var c = true;
console.log(a === b); //true
console.log(a == c); // True '==' will be cast
Copy the code

2. Reference type

Reference data types are collectively known as Object objects and include objects, arrays, functions, dates, and re

Store in the heap

Heap memory is unordered storage

A reference type is stored in heap memory, and a variable is actually a pointer in stack memory to an address in heap memory. Each space size is different, and the specific allocation should be made according to the situation. For example:

var person1 = {name: 'joj'};
var person2 = {name: 'xiaomi'};
var person3 = {name: 'xiaoyang'};
Copy the code

Value of the variable

Such as:

var a = [1.3];
a[1] = 2;
console.log(a); / / [1, 2]
Copy the code

Comparison of reference types is comparison of references

Every time we operate on a reference type in JS, we operate on a reference to its object (a pointer stored in stack memory), so we compare the two reference types to see if they point to the same object. Such as:

var a = [1.2.3];
var b = [1.2.3];
console.log(a === b); //false
Copy the code

Although the variables a and b represent the same content, they are not in the same location in memory. They do not refer to the same object, so they are not equal.

Data type judgment

typeof

Returns a string representing the data type. The returned results include: number, Boolean, string, symbol, object, undefined, function and other seven data types. Null, array, and other data types cannot be determined

typeof Symbol(a);/ / symbol is effective
typeof ' '; / / string is effective
typeof 1; / / number is effective
typeof true; / / a Boolean is effective
typeof undefined; / / undefined effectively
typeof new Function(a);/ / function effectively
typeof null; / / object is invalid
typeof[];/ / object is invalid
typeof new Date(a);/ / object is invalid
typeof new RegExp(a);/ / object is invalid
Copy the code

instanceof

If A is an instanceof B, A instanceof B returns A Boolean value. Instanceof tests whether an object has a constructor’s prototype property in its prototype chain, but it cannot detect null and undefined

[] instanceof Array; //true
{} instanceof Object;//true
new Date(a)instanceof Date;//true
new RegExp(a)instanceof RegExp//true
null instanceof Null/ / an error
undefined instanceof undefined/ / an error
Copy the code

Object.prototype.toString.call()

The most accurate and most commonly used

Object.prototype.toString.call(' ');// [object String]
Object.prototype.toString.call(1);// [object Number]
Object.prototype.toString.call(true);// [object Boolean]
Object.prototype.toString.call(undefined);// [object Undefined]
Object.prototype.toString.call(null);// [object Null]
Object.prototype.toString.call(new Function());// [object Function]
Object.prototype.toString.call(new Date());// [object Date]
Object.prototype.toString.call([]) ; // [object Array]
Object.prototype.toString.call(new RegExp());// [object RegExp]
Object.prototype.toString.call(new Error());// [object Error]
Copy the code

Type conversion

Turns a Boolean

In the conditional, undefined, null, false, NaN, “”, ±0 are converted to false, and all others are true.

Object to basic type

ToPrimitive > valueOf() > toString(). All methods can be overridden

var a = {
    valueOf() {
        return 1;
    },
    toString() {
        return 2; },Symbol.toPrimitive]() {
        return 3;
    }
}
a + 1; / / 4
a + '1'; / / 31
Copy the code

Object key name conversion

  • The key name of an object can only be a string or Symbol
  • Other key names are converted to strings
  • The toString method is called by default when an object is converted to a string

Chestnut 1:

var a = {}, b = '123', c = 123;
a[b] = 'b';

// The key name of c will be converted to the string '123', overriding b
a[c] = 'c';

/ / output c
console.log(a[b])
Copy the code

Chestnut 2:

var a = {}, b = Symbol('123'), c = Symbol('123');

// b is a Symbol type. No conversion is required
a[b] = 'b';

// C is a Symbol type. No conversion is required. The values of any Symbol type are not equal, so b is not overwritten.
a[c] = 'c';

/ / output b
console.log(a[b])
Copy the code

Chestnut 3:

var a={}, b={key:'123'}, c={key:'456'};  

// b is not a string and is not a Symbol type.
// The object type is converted to the string [object object] by calling the toString method.
a[b]='b';

// c is not a string and is not a Symbol, so it needs to be converted to a string.
// The object type is converted to the string [object object] by calling the toString method. I'm going to overwrite b here.
a[c]='c';  

/ / output c
console.log(a[b]);
Copy the code

Four operators

  • Addition operator: if one is a string, the other is converted to a string
  • Other operators: if one side is a number, the other side is converted to a number
1 + '1'; / / '11'
2 * '2'; / / 4
[1.2] + [2.1]; / / '1,22,1'
// [1, 2].tostring () -> '1,2'
// [2, 1].tostring () -> '2,1'
// '1,2' + '2,1' = '1,22,1'

/ / magic!
'a' + + 'b' // -> "aNaN"
// Because + 'b' -> NaN
// You may have seen + '1' -> 1 in some code
Copy the code

The = = operator

ToPrimitive () is used to convert objects to basic types

Chestnut:

// [] convert to true and then convert to false[] = =false
// According to Article 8
[] == ToNumber(false) [] = =0
// According to Article 10
ToPrimitive([]) == 0
// [].toString() -> ''
' '= =0
// According to Article 6
0= =0 // -> true
Copy the code

Comparison operator

  • If it’s an object, it passestoPrimitiveConverting objects
  • If it’s a string, it passesunicodeCharacter index for comparison