JavaScript primitive type conversion and binary conversion

1.JavaScript converter comprising: a cast and the basic conversion

Such as:

var  str = 'A',num=10,nu=null,t=true,und=undefined,x;

// Note: The definition x has not been initialized; default is undefined

Cast:

Number (str); // output: NaN because str is not a number

Number (undefined); // output: NaN because str is not a number

Number (null); // Output 0

Number (true); // Output 1

Number (false); // Output 0

 

String (num); // Output: "10" string

String (null); // Output: "null" strings

String (undefined); // Output: "undefined" string

String (true); // Output: "true" string

 

Boolean(0);// false

Boolean("");空 false

Boolean(null);false

Boolean(undefined);false

Non-mandatory conversion :( binary, decimal, octal, hexadecimal conversion)

parseInt (str, 16); // second parameter specifies the original binary string; 10 10 decimal final output format;

parseFloat (str); // NaN non-numeric

num.toString (2); // Output: binary string "1010";

num.toString (16); // outputs a hexadecimal

nu.toString (); // error; as null and undefined values ​​are not converted 

t.toString (); // Returns string "true"

Mandatory and fundamental differences conversion methods:

1. The entire cast is substantially converted value of the conversion is the conversion and the beginning of one part.

2. generally do not know in the end is what type of variable type; you can use casts

Such as: String (null), Boolean (null); as cast can be converted to any type of variable

 

Guess you like

Origin www.cnblogs.com/wanglijun/p/10958760.html