Value type conversion
Converting a value from one type to another is called a cast. This is the explicit case. The implicit case is called a cast, for example!
var a = 42;
var b = a + ""; // Implicit cast
var c = String(a); // An explicit cast
Copy the code
Abstract value operation
String String data type conversion
Use: [val].toString()
Null and undefined disallow toString and generate an error
Array: []=>”; [12] = > “12”.
The result of toString() is “[object object]”.
2. Use: String(…) function
If there is a toString() method, that method is called and the result is returned
If null, return null, if undefined, return undefined
Number Data type conversion
Use: the Number ()
String to number:
As long as any non-valid numeric character in the string (except the first dot, which represents a decimal point) results in NaN, the empty string becomes 0
Boolean type conversion to number:
True = > 1, false = > 0
null=>0
undefined=>NaN
Conversion of reference data types to numbers:
It is first converted to a string using the toString() method, and then to a number
Object: {} / {XXX: ‘XXX’}. The toString () = > “[object object]” = > NaN
Array: [] = > ‘= > 0; [1] = > ‘1’ = > 1; [1,2]=>NaN(because comma is not a valid numeric character)
ParseInt ()/parseFloat() works primarily with strings, and is a method of converting numbers. For strings, it looks for valid numeric characters from left to right until it hits an invalid numeric character (at which point it stops looking), and returns what it has found as a number. If there are no valid numeric characters, It returns NaN
NaN (short for not a number)
It’s not a number, but it’s a number type, and it’s not equal to any value, including itself, NaN, and any mathematical operation that results in NaN
IsNaN () : Used to test whether a value is a significant number, returning true if not and false if it is.
When testing with isNaN, it verifies that the value being tested is numeric, and if not, it converts the value to a numeric type based on the Number() method, and then verifies that isNaN(NaN)=>true
Boolean Boolean data type conversion
Only 0, NaN, “”, null and undefined are converted to false, and the rest are converted to true
Use: Boolean ([val])
Implicit type conversions are:
! /!!!!!
! First turn Boolean, then invert
!!!!! Taking inverts and taking inverts is essentially converting to Boolean
If (){}
Nul/undefined conversion
Null: “expected”, usually used as the initial null value, because 0 is not null and takes up space in stack memory
Undefined: “unexpected” (create variable without assigning value, default is undefined)
Explicit conversion between strings and numbers
This is done by String() and Number(), where String() follows the ToString rule to convert a value to a String primitive, and Number() follows the ToNumber rule to convert a value to a numeric primitive
Explicitly parses numeric strings
Parsing a number in a string and converting to a number return a number, but there are significant differences between parsing and converting, for example!
var a = "42"
var b = "42px"
Number(a) / / 42
parseInt(a) / / 42
Number(b) // NaN
parseInt(b) / / 42
Copy the code
Parsing allows non-numeric characters in strings, because parsing from left to right stops and returns the taken number when it encounters a non-numeric character, while conversion returns NaN when it encounters a non-numeric character, each serving its own purpose
Implicit casting between strings and numbers
To convert from a number to a string, use +””, if one of the operands of the + sign is a string, string concatenation is performed, otherwise digit addition is performed
To convert a string to a number, use -0. The -0 operation converts the operand to a string (toString()) and then to a number to perform subtraction
“= =” and “= = =”
Common mistake: “== checks if values are equal, === checks if values and types are equal”
This statement is incorrect, the correct interpretation is: “== allows casting in equality comparison, === does not allow”, for example!
var a = "42";
var b = true;
a == b // false
Copy the code
Why does “42” translate to Boolean return false when it is true? ToBoolean converts true to the number 1 and string “42” to the number 42. This will return false.
If Type(x) is Boolean, return ToNumber(x)==y
If Type(y) is Boolean, return x==ToNumber(y)
So the above example would have the same result even if reversed
Therefore, it is recommended not to use == in any case, for example!
if(a == true) // This will cause errors if the conditions are not established
if(a === true) // This will cause errors if the conditions are not established
if(a) // It works like this
if(!!!!! a)// It works better this way
if(Boolean(a)) // It works like this
Copy the code
Note that null and undefined result in true using ==, since undefined is derived from NULL
Comparison between objects and non-objects
If Type(x) is a string or number and Type(y) is an object, return x==ToPrimitive(y).
If Type(x) is an object and Type(y) is a string or number, ToPrimitive(x)==y is returned
Only strings and numbers are mentioned here, because booleans are cast to numbers
The ToPrimitive() method first checks to see if the value has a valueOf() method. If it does, the ToPrimitive() method returns a valueOf the primitive type and casts it. If it does not, the ToPrimitive() method casts the value returned by toString
Use implicit casts safely
- Do not use Boolean values on either side
= =
! - If the two values contain [],””, and 0, do not use them
= =
- Use === whenever possible to avoid accidental casts