There are two types of data type conversion in JS: forced type conversion and implicit type conversion. This article mainly shares with you a summary of JS data type conversion, hoping to help everyone.
are all numbers, direct operations
are all strings, use the corresponding characters ASCII value operation
One of them is a boolean value, convert the boolean value into a number, true->1 or false->0
One of them is a number, convert the other one into a number
If there is NaN, the result is NaN or false
Four operating functions are defined inside JS
ToPrimitive(obj, [PreferredType]) obj
: The object to be converted PreferredType
: The target type to be converted (only accepts Number and String types, the default is empty
), if obj is Date
, then PreferredType=String
, in other cases PreferredType=Number
##ToNumber(arguments)
ToString(arguments)
to Number
Convert to String
Related recommendations:
The above is the detailed content of Summary of JS data type conversion. For more information, please follow other related articles on the PHP Chinese website!