js data type replacement

js data type replacement

1. Convert to string type

1. Convert Number type to String type
2. Convert Boolean type to String type
3. Convert undefined type to String type
4. Convert null type to String type

In JavaScript, if you want to convert the above four basic data types to string types, there are three commonly used methods: 1.
For the Number type and Boolean type, you can use the variable name.toString() to convert
2. You can By String (constant or variable); converted to a string
3. It can also be converted to a string by variable or constant + "" / variable or constant + ''

2. Convert to numeric type

1. Convert the String type to a numeric type

  • If the strings are all numeric values, then convert them normally
  • If the string is an empty string ""/" ", it will be 0 after conversion
  • If there are not only numbers in the string, then NaN after conversion
    2. Convert Boolean type to numeric type
  • 1 after true conversion
  • 0 after false conversion
    3. Convert the undefined type to a numeric type
  • NaN after conversion
    4. Convert null type to numeric type
  • 0 after conversion

In JavaScript, if you want to convert the above four basic data types into numeric types, there are three commonly used methods:
1. Convert by Number (constant or variable);
2. You can also use the + and - signs in mathematical operations To convert
3. You can also pass parseInt (string to be converted)/parseFloat (string to be converted)

Summary:
Empty strings/false/null conversions are all 0
strings, not only numbers/undefined conversions are NaN
other normal conversions

3. Convert to Boolean type

1. Convert String type to Boolean type,
as long as there is content in the string, it will be converted to true, and only if there is no content in the string, it will be converted to false 2.
Convert Number type to Boolean type,
and only when the value is 0 will it be converted to false, Others will be converted to true
if it is NaN will also be converted to false
3. Convert undefined type to Boolean type
undefined will be converted to false
4. Convert null type to Boolean type
null will be converted to false

Summary:
Empty strings/0/NaN/undefined/null will be converted to false, and others are true.
If you want to convert basic data types to Boolean types in JavaScript, you only need to call Boolean (constant or variable)

Guess you like

Origin blog.csdn.net/hekai7217/article/details/119773712
Recommended