Data type conversion of JavaScript variables

The type conversion of variables is usually automatically converted by JS, but sometimes manual conversion is required.


##Convert other types to Boolean


First introduce a system built-in function Boolean(), which is used to force data into Boolean type. As for what a function is, we will introduce it later

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script>
            var x1 = "abc";       //true
            var x2 = "110";       //true
            var x3 = "";          //false
            var x4 = 110;         //true
            var x5 = 0;           //false
            var x6 = NaN;         //false
            var x7 = undefined;   //false
            var x8 = null;        //false
        //验证我们的注释结果是否正确
        //使用Boolean()全局函数,强制将变量转化成布尔型
        var result = Boolean(x1);
        //输出变量的类型和结果
        document.write(x1+"转布尔型的转换结果为:"+result);
        </script>
    </head>
    <body>
    </body>
</html>

Note: We only forced one conversion, you can also use the forced conversion function to convert the rest and view the output results

Convert other types into character types


This time we use the String() function to force other types into character types

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script>
            var x1 = true;       //true
            var x2 = false;       //false
            var x4 = 110;         //110
            var x5 = 0;           //0
            var x6 = NaN;         //NaN
            var x7 = undefined;   //undefined
            var x8 = null;        //null
        //验证我们的注释结果是否正确
        //使用String()全局函数,强制将变量转化成字符型
        var result = String(x1);
        //输出变量的类型和结果
        document.write(x1+"转字符型的转换结果为:"+result);
        </script>
    </head>
    <body>
    </body>
</html>

Note: Please test the others carefully. If you encounter unclear conversion in the future, please remember to use the forced conversion function




Convert other types to numeric types

We use the Number() function to force conversion of other types into characters Type

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script>
            var x1 = true;        //1
            var x2 = false;       //0
            var x3 = "120px";     //NaN
            var x4 = 100;         //100
            var x5 = "";          //0
            var x6 = undefined;   //NaN
            var x8 = null;        //0
        //验证我们的注释结果是否正确
        //使用Number()全局函数,强制将变量转化成数值型
        var result = Number(x1);
        //输出变量的类型和结果
        document.write(x1+"转数值型的转换结果为:"+result);
        </script>
    </head>
    <body>
    </body>
</html>



Continuing Learning
||
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script> var x1 = "abc"; //true var x2 = "110"; //true var x3 = ""; //false var x4 = 110; //true var x5 = 0; //false var x6 = NaN; //false var x7 = undefined; //false var x8 = null; //false //验证我们的注释结果是否正确 //使用Boolean()全局函数,强制将变量转化成布尔型 var result = Boolean(x1); //输出变量的类型和结果 document.write(x1+"转布尔型的转换结果为:"+result); </script> </head> <body> </body> </html>
submitReset Code