Js basic types and their conversion rules
Primitive value types refer to data types, eight basic data types that are easy to overlookBigInt
Raw value data type | Conversions between types |
---|---|
Number | Nuber(null) // 0 Number(undefied) // NaN Number([]) // 0 Number([0]) // 0 Number([1]) // 1 Number({}) -> NaN Number(“12px”) -> NaN Number (” 1.1 e+21 “) |
String | String(null) // ‘null’ String(undefined) // ‘undefined’ String([]) // ” String({}) // ‘[object Object]’ String([1,null,2,undefined,3]) // E20 String (11) / / “1.1 e+21” |
Boolean | Two cases are implicitly converted to Booleans 1. Logical operations: and or not (! &&!!!!! | |) If () {} / switch () Key points: There are 6 conditions known as falsy: NaN, undefined, null, 0, “”, false (note: these 3 special cases are true “false”, “”, “0”) |
Null | |
Undefined | |
Symbol | |
Bigint | The purpose of the BigInt data type is to have a wider range of integer values than the Number data type supports |
Object |
There are two special data types: null and undefined.
type | Definition: | use | Conversion to other types of values |
---|---|---|---|
null | Represents “null value”, represents a pointer to a null object, use typeof Operation to get"object" .So you can think of it as a special object value. |
1. As the end of the prototype chain Click on the default values of user-defined parameters, Represents the object type |
null == null //true null == undefined //true null == 0 // false null == false //false null == [] //false null == {} // false |
undefined | A variable was declared but not Assigns a value to this variable, which defaults to undefined |
1. Declare a variable without assigning a value, The default value is undefed 2. The function has no return value. The default value is undefined 3. When the function is called, no parameter is passed. The default value is undefined 4. An attribute of the object is not assigned and the default value is un______ |
undefined == undefined // true undefined == null //true dundefined == 0 //false undefined == false // false undefined == [] //false undefined == {} //false |
Conclusion: null and undefined are null and undefined except for themselves and each other.
Implicit conversion scenarios are common in js
1. if
conditional
if(xxx){};
switch(xxx){
case 1:
break;
default;
}
Copy the code
In general condition judgment, condition XXX will be converted to Boolean type, and then the judgment of the two sides of the equation
2. = =
Comparison operation> = < =
等
== comparison: If the two sides of an expression have different data types, they are implicitly converted to the same type and then compared
>= <= both strings will not convert the two-sided expression to Number, eg: “2” > “10” // true
- Object == string (in this case, objects are converted to strings before comparison)
- null == undefined
true
Note: null, undefined, and other values are not equal if there are three equal signs. - NaN == NaN
true
// If so= = =
It’s not equal - Symbol == Symbol
false
- Other cases [e.g. : object == number, string == Boolean…] They have to be converted into numbers and then compared
Here’s an example:
! [] = =false // true[] = =false // true
Copy the code
Why does it appear! [] and [] == true?
! [] === false Boolean([])) == Number(false) 2. [] === false
3. The plus sign +
With a minus sign-
+
In addition to implicitly converting to Number, there are alsoString splicing
The function of
Common conversions to rules:
expression | The output |
---|---|
1 + 1 | 2 |
1 + 1 “” | “11” |
1 – “1” | 0 |
-
Common difference between n++ and ++n
-
+n converts to a number
-
++n converts to a number and adds up to 1
-
N++ converts to a number and then adds up to 1
Three chestnuts, please:
CASE1: the following three expressions I ++, I += 1; Is I equal to I plus 1 the same?
expression | |
---|---|
i++ | Convert to a number and add up to 1 |
i += 1 | It could be string concatenation |
i = i +1 | It could be string concatenation |
So: 3 expressions are not equal, and only I +=1 is equal to I = I +1
CASE2: + has an object on one side
let n = 10;
{} + n // 10, {} will be treated as fast code, do not participate in the operation, only +n
n + {} // "10[object object]" string splicing
Copy the code
Important point of CASE3: Not all objects are directly converted to strings and then concatenated, but rather
- Go fetch the object first
Symbol.toPromitive
Property value if there is no property - To call the object’s
valueOf
Gets the original value, if not the original value - To call the object’s
toString
Convert to a string (if you want to convert to a Number)
console.log(10 + [10.20]) // 1010,20 string concatenation
/ / 1. [10, 20] [Symbol. ToPromitive]
/ / 2. [10, 20]. The valueOf ()
// 3. Finally convert to string
Copy the code
Two test questions: What does the following expression output?
let i = 10;
console.log(5 + (i++)) // 15, ++ does not take effect the first time
console.log(5 + (++i)) // 17, I = 11 after ++, I = 12
console.log({} + i) ; // 12, {} will be used as a code block
console.log(i + {}); //"12[object Object]"
Copy the code
How do I change 10 + {a: 10} to 20
let obj ={
a:10[Symbol.toPrimitive](hint){
console.log("hint",hint) // "default", 'string', 'number'
return this.a; }}console.log(10+obj) // This can be done using several CASE3 apis or setters
Copy the code
What are the built-in object types
Object type | Reference data type |
---|---|
Standard Object types: Object, Array, RegExp, Date, Math, Error, ArrayBuffer, DataView, Set, Map | |
Nonstandard special objects Number, String, Boolean, Symbol, Bigint… Based on the format information of the original value object type created by the constructor or object, the type is an object type | |
Callable execution object: implements callable object function |
What are the methods of data type detection
Three ways to detect data types
methods | function |
---|---|
typeof | Identify simple data types number, string, undefined, symbol, function Typeof typeof [1,2,3] returns a string typeof typeof [1,2,3] It returns the string string Disadvantages: Inability to distinguish specific reference types |
instanceOf | |
Object.prototype.toStirng.call(value) |
Use the Typeof checklist
- The underlying mechanism of Typeof is to determine that these binary, 000 starts with objects and excludes functions
Undefined | ‘undefined’ |
---|---|
Null | ‘object’ |
Boolean | ‘boolean’ |
String | “string” |
Symbol | “symbol” |
Object(ordinary and does not implement[[call]]) | “object” |
Ojbect(stardard exotic and does not implement[[call]]) | “object” |
Object(implement[[call]]) | “funciton” |
Object(non-stardard exotic and does not implement[[call]]) | Implement -defined. Must not be ‘undefined’, ‘Boolean’, ‘function’, ‘number’, ‘symbol’, or ‘string’ |
An undeclared variable | undefined |
Typeof variables that are not declared do not report errors
Computer Science: Computer principles, base conversion, computer networks, data structures and algorithms
000 | object |
---|---|
1 | The integer |
010 | Floating point Numbers |
100 | string |
110 | Boolean |
000 | object |
000000 | null |
– 2 ^ 30 | undefined |
Characteristics 1:
Typeof null -> "object" typeof null -> "object" Typeof new RegExp(/b/g) : 'object' typeof new RegExp(/b/g) : 'object'Copy the code
Character 2:
typeofObject - >"object" && typeofFunction - >"function"
Copy the code
Feature 3: Typeof undeclared variables -> “unfedined” : How to use this feature (Scenario example: Exposed API in plug-in packaging)
(function () {
let utils = {
};
// What's wrong with this? If a variable does not exist, typeof can be used to determine
if (typeof window! = ='undefined') window.utils = utils;
// common.js specification export
if (typeof module= ='object' && typeof module.exports === 'object') moudule.exports = utils; }) ()// Browser only
// utils.xxx()
Copy the code
Fast method for detecting number types
api | role |
---|---|
Array.isArray() | Check whether it is an array |
isNaN | Is it not a number |
A special data typeSymbol
Data type if get?
- How does Symbol get unique values
let n = Symbol('AA');
let m = Symbol("AA");
console.log(n == m) //false
let key = Symbol(a);let obj = {
[key]:100
}
console.log(obj[key]);
Copy the code
- What are the common APIS of Symbol?
Symbol.hasInstance
Symbol.iterator
Symbol.toPrimitive
Symbol.toStringTag
Convert other types to Object([value])
Rule: Raw conversion is directly enclosed in quotes [bigint removes n]
expression | |
---|---|
(Symbol()).toString() | “Symbol()” |
(10n).toString() | “10” |