preface
Js data types are often asked in front-end interviews. Next, I will talk about my understanding of data types
Js data types
Primitive data type
Primitive data types: number, string, Boolean, null, undefined, symbol, BigInt
Note: Symbol is new to ES6 and represents unique values. BigInt is also new
Reference types
Reference types: function, object, Array
Weakmap Weakset RegExp Date Math etc are also reference types
How to detect data types using Typeof and Instanceof
typeof
Typeof: JavaScript unary operator used to return the original typeof a variable as a string
console.log(typeof 1) // 'number'
console.log(typeof '1') // 'string'
console.log(typeof undefined) // 'undefined'
console.log(typeof true) // 'boolean'
console.log(typeof Symbol(a)// 'symbol'.console.log(typeof null) // 'Object'
console.log(typeof {}) // 'Object'
console.log(typeof [1.2.3]) // 'Object'
console.log(typeof function) / / 'function'
Copy the code
Note: typeof null // ‘object’, so null cannot be determined, this is a bug in js. In JavaScript, the first three bits of a binary are all zeros, and the first three bits of a binary are all zeros. Therefore, “Object” is returned when typeof is executed.
Note: Typeof reference type is not easy to determine, typeof “reference type” except function can be defined as ‘function’, other ‘Object’.
instanceof
Instanceof: JavaScript operator that searches through the constructor in the prototype chain and returns true if found, false otherwise
let a = { name: 'true' }
let b = [1.2.3]
let c = function() {}...console.log(a instanceof Object) // true
console.log(b instanceof Array) // true
console.log(c instanceof Function) // true
console.log(b instanceof Function) // false
console.log(a instanceof Array) // false.Copy the code
Conversion between js data types
Display type conversion:
Number()
In:ES5
Abstract operations are defined in the specificationToNumber
- A value of Undefined is converted to
NaN
- A value of type Null is converted to 0
- String value conversions are performed using the Number() function, or to if they contain non-numeric values
NaN
, the empty string is 0 - Boolean values, true to 1 and false to 0
- Values of type Symbol cannot be converted to numbers and an error is reported
- A value of Undefined is converted to
console.log(Number()) / / 0
console.log(Number(null)) / / 0
console.log(Number(false)); / / 0
console.log(Number(true)); / / 1
console.log(Number(123)); / / 123
console.log(Number(000123)); / / 123
console.log(Number(' ')); / / 0
console.log(Number(undefined)); //NaN
console.log(Number('aaaa')); // NaN
console.log(Number(Symbol())); // TypeError
Copy the code
String()
: can be converted to strings
console.log(String(123)) / / '123'
console.log(String(null) // 'null'
console.log(String(a)) // 'a'
console.log(String(undefined)) // 'undefined'
console.log(String(NaN)) // 'NaN'.Copy the code
toString()
: undefined and null cannot be converted
console.log(toString(123)) / / '123'
console.log(toString(null) // 'null'
console.log(toString(a)) // 'a'
console.log(toString(undefined)) // 'undefined'
console.log(toString(NaN)) // 'NaN'
console.log(toString(undefined)) // [object Undefined]
console.log(toString(null)) // [object Undefined]
Copy the code
boolean()
: a Boolean value
console.log(Boolean()) // false
console.log(Boolean(false)) // false
console.log(Boolean(null)) // false
console.log(Boolean(undefined)) // false
console.log(Boolean(NaN)) // false
console.log(Boolean(-0)) // false
console.log(Boolean(+0)) // false
console.log(Boolean(' ')) // false
console.log(Boolean(1)) // true
console.log(Boolean('a')) // true
console.log(Boolean({})) // true
console.log(Boolean([])) // true
Copy the code
Boolean() : 0 “(empty string) null undefined NaN will be converted to false everything else will be converted to true
Implicit type conversion:
Unary operator
The: unary operator calls TONumber by default to handle this value. If the value passed in is an object type, the ToPrimitive() method is called first, doing the following:- If the value is a primitive type, it is returned directly
- Otherwise, the valueOf method is called, and if a raw value is returned, js returns its value
- Otherwise, the toString method is called, and if a raw value is returned, js returns its value
- Otherwise, type error is reported
console.log(1 + '1'); / / '11'
console.log(+'1'); / / 1
console.log(+ []); / / 0
console.log(+ ['1']); / / 1
console.log(+ ['1'.'2']); // NaN
console.log(+ {}); // NaN
Copy the code
Binary operator
For example, val1 + val2 follows the following rules:- v1 = ToPrimitive(val1)
- v2 = ToPrimitive(val2)
- If v1 is a string or v2 is a string, return the concatenation of ToString(v1) and ToString(v2)
- Otherwise, ToNumber(v1) and ToNumber(v2) are returned
console.log([] + 1) // 0 + 1 => 1
console.log(null + 1) // 0 + 1 => 1
console.log(1 + true);// 1 + 1 => 1
console.log(1 + '1') / / '11'.Copy the code
== == == = not equal
When converting operand types, follow the following rules:- If any of the operands is a Boolean, it is converted to a value and then compared for equality. Fase turns to 0 and true turns to 1
- If one of the operands is a string and the other is a numeric value, an attempt is made to convert the string to a numeric value and compare for equality
- If one operand is an object and the other is not, the valueOf() method of the object is called to get the original value, which is compared according to the previous rules
- The == operator returns false if any operand is NaN,! The = operator returns true
- If both operands are objects, compare them to see if they are the same object. Equality returns true if both operands refer to the same object. Otherwise, the two are not equal
Note: The equality operator returns false even if both operands are NaN, because NaN is not equal to NaN by rule
Note: undefined is special and follows the following rules: 1, null is equal to undefined. 2. Nul and undefined cannot be converted to other types of values for comparison
console.log( null= =undefined) // true
console.log( "NaN == NaN ) // false console.log( 1 == NaN) // false console.log( NaN == NaN) // false console.log( NaN ! = NaN) // true console.log( false == 0) // true console.log( true == 1 ) // true console.log( true == 2 ) // false console.log( undefined == 0) // false console.log( null == 0 ) // false console.log( '1' == 1 ) // trueCopy the code
= = = are congruent
与! = = not congruent
: and the previous == and! = compare, comparing data types
let a = ('11'= =11) // true, equal after conversion
let b = ('11'= = =11) // false, unequal because of different data types
let c = ('11'! =11) // false, equal after conversion
let d = ('11'! = =11) // true, not equal, because the data type is different
Copy the code
Boolean value
和Other types of
: When a Boolean value is present on one side, the value on that side is ToNumber()
console.log(1 + true) / / 2
console.log(1 - true) / / 0
console.log(1 + false) / / 1
Copy the code
conclusion
This is my understanding of javascript data types, and I welcome you to point out any mistakes. This is my first article, I hope you can support and like it. Thank you.