Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money

During development, we often encounter that the current value is not the type we expect, so we need to do a manual conversion. There’s more than one way to switch, so which one do you usually use? Why is that?

Ask questions

PS: The answers are all in the text

👉 String(null) and (null).toString()?

👉 1.tosting () and (1).tostring () why is the result different?

👉 parseFloat(“123.45.67”) and Number(“123.45.67”)?

👉 Previous review: JS operators you didn’t know!

The campaign needs to collect 🥂 [10 comments] 🥂, the current progress is still zero 😭, so


JS other types are converted to strings

Display conversion String(value)

The String(value) function can convert any type of numeric value to a String, including null and undefined

let value = true; console.log(typeof value); // boolean value = String(value); // Now, the value is a string of "true" console.log(typeof value); // stringCopy the code

The toString () method

The toString() method returns a string representation of the corresponding value

let value = true; value = value.toString(); // String "true"Copy the code

Values, booleans, objects, and string values all have toString() methods, but null and undefined do not and will report an error

PS: If it is a numeric value, the toString() method will return an error!

Reason: The JavaScript interpreter steals the “.” after the number (as the decimal point of the preceding number), similar to the following:

Use + “”

Add the converted value to a string “”


JS other types are converted to numeric types

Display convert Number(value)

Can be used to convert any data type to a value

let str = "123"; console.log(typeof str); // string let num = Number(str); // Change to number type 123 console.log(typeof num); // numberCopy the code

Number type conversion rule:

value To the result of type number
undefined NaN
null 0
True and false 1 s and 0 s
string The number contained in a purely numeric string with the leading and trailing Spaces removed.

👉 If the remaining string is empty, the result is converted to0.

👉 Otherwise, the number is “read” from the rest of the string.

👉 returns if a type conversion error occursNaN.
// 123 console.log( Number(" 123 ") ); Console. log(Number("123z"));Copy the code

Conversion functions parseInt() and parseFloat()

Js provides two conversion functions parseInt() and parseFloat(). Used specifically to convert strings to numbers.

  • ParseInt () converts the value to an integer

    console.log(parseInt("Axjy01"))  //NaN
    console.log(parseInt("01Axjy"))  //1
    Copy the code

  • ParseFloat () converts values to floating point numbers

The parseFloat (" 123.4567 ") / / 123.4567 parseFloat (" 123.. 4567") //123 parseFloat(" qWER123.4567 ") //NaN parseFloat("123.45.67") //123.45Copy the code


🍒 looks at each character starting at position 0 until the first invalid character is found, and then converts the string preceding that character to a number; 🍍 The first decimal point that occurs is a valid character. If there are two decimal points, the second decimal point is considered invalid

Using js variable weak type conversions (not recommended)

In arithmetic functions and expressions, number type conversions are performed automatically.

For example, when division/is used for non-number types

Console. log("6"/"2"); console.log("6"/"2");Copy the code

This feature can be used to perform operations such as subtracting 0 (not recommended)

Var STR = '012.345'; console.log(typeof str); // string str = str-0; Console. log(typeof STR); // numberCopy the code


Boolean conversion

Show transform Boolean(value)

Boolean type conversion rules:

value Cast to Boolean result
0, null, undefined, NaN, “” false
Other values true

Note:

console.log( Boolean(0) ); // false console.log( Boolean("0") ); // true console.log( Boolean("") ); // false console.log( Boolean(" ") ); // Blank, also true (any non-empty string is true)Copy the code


conclusion

Articles will be updated, optimized, and refactored constantly!

Type Conversions



🎨 [thumbs up] [concerns] don’t get lost, more front-end dry goods waiting for you to unlock

Phase to recommend

👉 What’s new with the latest version of chrome developer tools?

👉 layui image collection + how to copy the whole website down

👉 The seven types of JavaScript fill in the gaps

Memo | 👉 HTTP status code is commonly used & request & response header & Cookies and collection request method