first, the conversion of the basic type 1. numeric to string
A string that directly adds an empty number.
var num=20;
var str=20+ "";
Console.log (typeof str);//string
Use the string () function to make the number more visible to the string
var num=20;
var str=string (num);
Console.log (typeof str);//string
Use the ToString () method. ToString () has a parameter that specifies the cardinality of the Transformation. (decimal/binary/eight Binary/16-binary)
var num=20;
var str=num.tostring (10);
Console.log (typeof str+ "," +str);//string,20
2. String to number (conversion function, coercion type conversion, JS variable weak type Conversion)
parseint () and parsefloat () functions.
//parseint (), Two parameters, first string to be converted, second is the cardinality of the specified conversion (can be omitted)/*when the second argument is omitted, str begins with "0x", and the integer that resolves to 16 is preceded by "0", and an integer of EIGHT/16 is parsed as 1~9, parsing to a decimal integer*/varStr= "123hahaha";varnum=parseint (str); console.log (num+","+typeofnum);//123,numberConsole.log (parseint ("22.5"));// aConsole.log (parseint ("0xA"));//TenConsole.log (parseint ("blue"));//NaN//parseint can also be used to convert decimals to Integers.Console.log (parseint (233.45));//233
Parsefloat No Base mode
Console.log (parsefloat ("22.5hahaha"));//22.5
Force type conversion number (value) to convert a value to an integer or float
Console.log (number ("123.99haha")); // NaNconsole.log (number ("123.9")); // 123.9
JS Weak type conversion
var str= "123"; var n1=str-0; var n2=str*1; var n3=str+0; // the equivalent of adding a string to the number, so it is still the string console.log (n1+ "," +typeof n1); // 123,numberconsole.log (n2+ "," +typeof n2); // 123,numberconsole.log (n3+ "," +typeof n3); // 1230,string
3. Other types of conversions
// boolean (value);//convert The value to a Boolean value Console.log (Boolean (0)) // false;
// array-string // join (), array of methods, put the elements of the array in a string (the element is separated by the characters in parentheses). var arr=[2,3,4,5]; var str1=arr.join (" var str2=arr.join ("-" + "," +typeof str1); // 2345,string console.log (str2+ "," +typeof str2); // 2-3-4-5,string
// Array of strings // the concat () array method, joins the array, and returns the Result. Split () array method, dividing the array var str= "abcdef" in the specified way; var a1=[].concat (str); var a2=str.split (""); //console.log (a1+ "," +typeof a1); // abcdef,objectconsole.log (a2+ "," +typeof a2); // A,b,c,d,e,f,object
Binary Conversion 1. Decimal to another binary
Use the ToString
varNum=20;//in order not to change the type need to add parseintvarA=parseint (num.tostring (2));//decimal---binary (divided by 2 to take the remainder, backwards Upwards)varB=parseint (num.tostring (8));//Decimal---octal (divided by 8 to take the remainder, backwards Upwards)varC=parseint (num.tostring (16));//decimal---16 binary (divided by 16 to take the remainder, backwards up the number)Console.log (a + "," +typeofa);//10100,numberConsole.log (b + "," +typeofb);//24,numberConsole.log (c+ "," +typeofc);//14,number
2. Other binary decimal
var d=parseint (10100,2); // Decimal in binary var e=parseint (24,8); // octal, Decimal var f=parseint (14,16); // hexadecimal ---decimal console.log (d+ "," +e+ "," +f "); // 20,20,20
3. Other binary conversions
//octal, hexadecimal --binaryvarA=parseint (257,8). toString (2);varB=parseint ("af", +). toString (2); console.log (a+","+typeofa);//10101111,stringConsole.log (b + "," +typeofb);//10101111,string //binary, Octal-hexadecimalvarC=parseint (10101111,2). toString (16);varD=parseint (257,8). toString (16); console.log (c+","+typeofc);//af,stringConsole.log (d+ "," +typeofd);//af,string////binary, Hex --octalvarE=parseint (10101111,2). toString (8);varF=parseint ("af", +). toString (8); console.log (e+","+typeofe);//257,stringConsole.log (f+ "," +typeoff);//257,string//change the type of the value yourself as needed
Conversion of basic types in js, binary conversion