Basic data type conversion

First, cast

1. Other type conversion String

a.toString (); generates a new string does not change the original data type.
a.toString (2); converted into a binary string.
a.toString (8); octal string.
String (a); cast string type;

Difference between the two: x.toString () can not be converted null and undefined, For the conversion, the need to rewrite toString method.

window.onload=function(){
        var a;
        var b=null;
        //console.log(a.toString(),b.toString());//报错
        //console.log(String(a),String(b));

        function String(x){
                if(x===undefined){
                        return "undefined";
                }else if(x===null){
                        return "null";
                }else{
                        return x.toString();
                }
        }
}

2. Other types of transfer Number

Number(b);
true , 00001 ---> 1;
null , "" , [] , [""] , false , " " , [" "] ---> 0;
其他进制 ---> 十进制
undefined , 123abc , "abc" , ["web"] , ["1","2"] --->NaN
["10"] , "10" ---> 10

3.parseInt & parseFloat

parseInt("123abc") , parsFloat("123abc") ---> 123
parseInt("Fd123") , parseFloat("Fd123") ---> NaN
parseInt("0xf") ---> 15
parseFloat("0xf") ---> 0
parseInt(7.5) ---> 7
parseFloat(7.0) --->7

Second, the implicit conversion

1. Digital encountered string operator will automatically turn into type Number. (+, -, *,% /,>, <, =)
2 + before string, then the latter will be automatically converted to Number type String.
3.null, false, "", " " Switch numerical computation 0:00; 1:00 into true value is calculated.
4.undefined ---> NaN

Guess you like

Origin blog.51cto.com/11569511/2417810