This article mainly introduces the conversion of data types in javascript, which has some reference value. Let's take a look at it below. 1. convert other data types to the number type.
Number ()-> strict
ParseFloat/parseInt-> non-strict
IsNaN (value) if the value is not of the numeric type, it first calls the Number to convert it to the numeric type and then determines whether it is a valid Number.
For example:
Number ("12px");-> NaN
ParseInt ("12px");-> 12
IsNaN ("12");-> false
Rule:
1) if the boolean type is converted to a number, true-> 1 false-> 0
2) convert null/undefined to the number null-> 0 undefined-> NaN 10 + null = 10 10 + undefined = NaN
3) to convert [] to a Number, first call the toString method of the array to []-> "", and then use the Number method to convert "-> 0
2. convert other data types to boolean ones.
Boolean
!!
! Convert to a boolean type first, and then
For example:
!! 1-> true
!! 0-> false
! []-> False !! []-> True
Rule:
Only the values 0, NaN, null, undefined, and "" are converted to the boolean type as false, and the other values are converted to true.
Special cases:
If your condition judgment only contains a single value, it first converts 1 to the Boolean Type (-> true) and determines whether it is true or false. If it is true, otherwise, the condition is invalid.
If (1) {} if ("3px" * 3) {//-> the condition is not true. "3px" * 3 = NaN-> false} if ("3px" + 3) {//-> condition is set to "3px" + 3 = "3 p x 3"-> true}
"+" In JS, it is not just a mathematical operation, but a String concatenation in case of a string
"-, *,/" Can only be mathematical operations in JS. If it is not a number type, it must be forcibly converted to a number for mathematical operations.
3. Compare the two values (==== compared, ==== compared data types are not converted by default). If the data types on the left and right sides are different, we need to follow a set of rules, convert the default data type
Value1 = value2
1) object = The object compares the memory address stored by itself. If the memory address is different, it is not equal [] = []-> false. Each empty array is a separate memory address.
2) object = string call toString method, convert the object to string [] = ""-> true
3) object = convert a Boolean object to a Number (toString-Number), and convert a Boolean object to a Number (true-> 1 false-> 0)
[] = False-> true []-> 0 false-> 0 0 = 0-> true
! [] = False->! [] Convert an empty array to a Boolean array first, and then convert it to the inverse type-> false = false-> true.
4) convert object = numeric object to numeric [] = 0-> true [] = 1-> false
5) both string = Boolean are converted to numbers "3" = true-> false "1" = true-> true
6) convert string = numeric string to numeric "" = 0-> true
7) Boolean = numeric Boolean to numeric true = 1-> true = 2-> false
8) null = undefined-> true null = undefined-> false
Null/undefined is not equal to any other value. null = 0-> false
9) NaN = NaN-> false NaN and any value (including itself) are not equal
The above is all the content of this article. I hope this article will help you in your study or work, and I also hope to support PHP!
For more information about data type conversion in javascript, refer to the PHP Chinese website!