Home >Web Front-end >JS Tutorial >Javascript conversion method for operations between different types of data_javascript skills
Different types of basic data in js can be converted. This conversion is rule-based and not random. There are 5 basic types of data in js: string, number, boolean, null, and undefined. Among them, the first three types are commonly used for calculation or comparison.
Conversion between basic data
原始数据类型 | 目标类型Number |
undefined | NaN |
null | 0 |
false | 0 |
true | 1 |
数字串 | 相应的数字 |
不能转化的字符串 | NaN |
原始数据类型 | 目标类型String |
undefined | undefined |
null | null |
false | false |
true | true |
数字 | 数字字符串 |
Addition between different types of basic data, the data is first converted to number, and then converted to string (if string type data is involved in the operation)
Objects participate in addition and subtraction operations
Objects participate in basic type data operations and are first converted into basic types. First call its valueOf method. If the returned value is not a basic type, then call its toString method. If the returned value is not a basic type, an error will be thrown. However, Date data is just the opposite
Replace the above example with Object or other types to get the corresponding results, but call valueOf first and then call toString.
The magical effect of ' '
There is a plus sign ‘ ‘ in front of the data, which can convert the string into a number