The original reference
The way of front-end interview -JS- Type conversion
The text summarizes
Implicit conversion: When JavaScript attempts to manipulate an “incorrect” data type, it automatically converts to the “correct” data type, known as an implicit conversion.
The original article was intended to list several cases of implicit conversions, rather than active conversions, which are covered in an extension of this article.
Key points of the original text:
1. When a Boolean value is required but a non-Boolean value is written instead for code purposes: all values except undefined, null, false, NaN, “”, 0, -0 are automatically changed to false, and all values are changed to true.
2. When an object converts a primitive type, valueOf is first called and then toString is called.
document.getElementById("demo").innerHTML = myVar;
myVar = {name:"Fjohn"} // toString converts to "[object object]"
myVar = [1.2.3.4] // toString converts to "1,2,3,4"
myVar = new Date(a)// toString converts to "Fri Jul 18 2014 09:08:55 GMT+0200"
Copy the code
3. The four operators sometimes trigger the conversion mechanism, and only when an addition operation converts one of them to a string will the other be converted to a string. As long as one of the other operations is a number, the other becomes a number. Keep in mind the following examples:
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,2,1'
'a' + + 'b' // -> "aNaN"
// because + 'b' -> NaN
You may have seen + '1' -> 1 in some code
Copy the code
4. If an interviewer asks you a question about “==” in an interview, you can answer it directly and firmly: “==” can cause a lot of uncontrollable mistakes (there are too many, so there is no need to write it down, the interviewer knows ~), so I will only use “===” instead of “==” when determining equal values.
Original memory point
-
Undefined, null, false, NaN, ”, 0, -0 => false; The rest = > true
-
1 + ‘1’ // ’11’
-
2 * ‘2’ // 4
-
[1, 2] + [2, 1] // ‘1,2,1’
-
+ ‘1’ / / — > 1
-
‘a’ + + ‘b’ // -> “aNaN”
-
Instead of ==, just ===
Extension – Active (explicit) type conversion
Any type to a string
-
The global function String() can be used to convert any type of number, letter, variable, expression to a String
-
The.toString() function: Types with this built-in function are converted to strings
// Number to string
// Global String () function
String(x) // Convert variable x to a string and return
String(123) // Converts the number 123 to a string and returns
String(100 + 23) // Converts a numeric expression to a string and returns it
// Number's toString() method
x.toString()
(123).toString()
(100 + 23).toString()
// Other methods
(1.111).toFixed(2) // Return "1.11" to convert a number to a string with the specified number of digits behind the decimal point
(2.222222222).toExponential(4)// return "2.2222e+0" to convert the value of the object to an exponential
(123456789).toPrecision(3) // return "1.23e+8" to format the number to the specified length
// Boolean to string
// Global String () function
String(false) / / return "false"
String(true) / / return "true"
// Boolean toString() method
false.toString() / / return "false"
true.toString() / / return "true"
// Date to string
// Global method String()
String(new Date())
// Date's toString () method
obj = new Date()
obj.toString()
// Other methods
obj.getDate() // Return "17" returns a day in a month (1-31)
obj.getDay() // Return a day of the week (0 ~ 6)
obj.getHours()// Return hour (0 ~ 23)
Copy the code
Any type to a number
The global method Number() can be used to convert any type of Number, letter, variable, or expression to a Number
// String to number: empty string is converted to 0; Non-numeric strings are converted to NaN.
/ / the Number () function
Number("3.14") / / return 3.14
Number("") / / returns 0
Number("") / / returns 0
Number("99, 88") / / returns NaN
// Other methods
parseFloat('1.11') // Returns 1.11 parsing a string and returning a floating point number
parseInt('1.11')// Returns 1 parses a string and returns an integer
var y = "5"; // y is a string
var x = + y; // x is a numeric unary operator + that can be used to convert variables to numbers
// Boolean turns to a number
Number(false) / / returns 0
Number(true) / / returns 1
// Convert the date to a number
d = new Date(a);Number(d) / / back to 1404568027739
d.getTime() / / back to 1404568027739
Copy the code