Today I will introduce to you the knowledge of implicit conversion in JavaScript. I hope it will be helpful to everyone in learning JavaScript.
Implicit type conversion in Javascript is an internal implicit call to the displayed method, which converts the unwanted value type into the desired type. For example, if you pass a string, but you want a number or object, implicit type conversion will convert it to the correct type, and this is an internal conversion of the system, so even if it is converted, we don't know
(1)Non-numeric values in numeric expressions
The conversion process of numbers is similar to Number calling the built-in function on the value, that is, any string containing only numeric characters will be converted to their equivalent numbers, but a string whose return value contains non-numeric characters will return NaN
Example: Convert a string to a number, but if it does not contain pure numbers, NaN will be returned
Number("1") // 1 Number("1+1") // NaN Number("1a") // NaN Number("text") // NaN
(2) Operator case
Symbolic operation has two functions , one represents the addition of numbers, and the other represents the connection between strings
Example: When numbers are added to numbers, it represents mathematical addition, when numbers are added to strings, it represents connection
1 + "2" // "12" 1 + "js" // "1js" 1 + 2 // 3
(3) Object
In most cases, Javascript object conversion will result in [object Object]
Example: a string is compared with an object Add
"obj" + {} // "obj[object Object]"
Note: Each javascript object in the program will inherit a toString method. As long as the object is converted to a string, this method will be called
(4) Array object
When an array of strings is required by calling the join array method without any parameters, Javascript concatenates the return value of the toString method with the second operand. If a number is required, an attempt is made to convert the return value to a number.
[1,2,3].toString() // "1,2,3" [1,2,3].join() // "1,2,3" 4 + [1,2,3] // "41,2,3" 4 * [1,2,3] // NaN
Summary: The above is about implicit type conversion in JavaScript. I hope that this article can help everyone understand implicit type conversion.
The above is the detailed content of What is invisible type conversion in JavaScript. For more information, please follow other related articles on the PHP Chinese website!