Three types of JS conversion number/string/Blooean

1.
There are three ways
to convert other types to digital types : 1. Convert integer parseInt(string, radix); (string must be filled, radix can be omitted, radix is ​​the base of the number to be parsed, and the value is between 2 ~ 36 It is binary, octal, decimal, and hexadecimal.) Parse a string and return an integer number. It starts from the first character to determine whether it is a number. If it is a number, it continues with the second character until Not so far. If the first character is not a number, NaN is returned.

        console.log(parseInt("10"));//10
        console.log(parseInt("10afrswfdsf"));//10
        console.log(parseInt("g10"));//NaN
        console.log(parseInt("1fds0"));//1
        console.log(parseInt("10.98"));//10
        console.log(parseInt("10.98fdsfd"));//10

result:
Insert picture description here

2. Convert to decimal parseFloat(string); Parse a string parameter into a floating-point number and return it. If a character other than sign, number (0-9), decimal point, exponent e or E is encountered during the process, it will be ignored The character and all the characters after it, return the floating-point number currently parsed. If there are two decimal points, such as 25.3.5, then return 25.3. If the first character of the string cannot be converted into a number, NaN is returned.

        console.log(parseFloat("-10.985"));//-10.985
        console.log(parseFloat("25.3.5"));//25.3
        console.log(parseFloat("10"));//10
        console.log(parseFloat("10afrswfdsf"));//10
        console.log(parseFloat("g10"));//NaN
        console.log(parseFloat("1fds0"));//1
        console.log(parseFloat("10.98"));//10.98
        console.log(parseFloat("10.98fdsfd"));//10.98

Result:
Insert picture description here
3. The Number(object)
function converts the value of the object to a number. If it cannot be converted to a number, it returns NaN; if the parameter is a Date object, it returns the number of milliseconds from 1970/1/1 to the present.

        var date1 = new Date();
        console.log(Number (date1));//从1970年一月一日至今的毫秒数
        console.log(Number("10"));//10
        console.log(Number("10afrswfdsf"));//NaN
        console.log(Number("g10"));//NaN
        console.log(Number("1fds0"));//NaN
        console.log(Number("10.98"));//10.98
        console.log(Number("10.98fdsfd"));//NaN

Result:
Insert picture description here
Summary:
Use parseInt() if you want to convert integers, use parseFloat() if you want to convert decimals; use Number() if you want to convert numbers, but Number() is more strict, so choose according to the actual situation.

Second, other types to string type
1. ToString()
variable is meaningful to call.toString() uses conversion.toString
() A number can be written in the brackets, which represents the hexadecimal system, and the corresponding hexadecimal string
toString() can convert all The data is converted to a string, except for null and undefined, null and undefined
are generally not converted to a string, so toString() is used most of the time

var num=10;
console.log(num.toString());//10

Result:
Insert picture description here
undefined

var num1;
console.log(num1.toString());

Insert picture description here
null

var num2 = null;
console.log(num2.toString());

Insert picture description here
2 String();
If the variable is meaningless, use String() conversion, you can convert all the variables to strings.
Web page code String() can convert null and undefined to strings, but it can’t convert strings.

var num1=20;
console.log(String(num1));//20

Insert picture description here

Three, other types to Boolean type
1 Boolean (value);
to remember, these conversions are more commonly used in judgment

console.log(Boolean(1));//true
console.log(Boolean(0));//false
console.log(Boolean(11));//true
console.log(Boolean(-10));//true
console.log(Boolean("哈哈"));//true
console.log(Boolean(""));//空字符串 false
console.log(Boolean(null));//false
console.log(Boolean(undefined));//false

result:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44401120/article/details/92830625