JavaScript資料型別強制轉換

2021-08-28 08:44:50 字數 3217 閱讀 1187

我的個人部落格

基本資料:number, boolean, undefined, null, string;

引用資料:object;

當0.000…01,小數點後大於等於7位時會自動轉換成科學計數法

當20000…00,當整數部分大於等於22位時會自動轉換成科學計數法。

console.log(number(1));        //1

console.log(number(1e-7)); //1e-7

console.log(number(0b111)); //7

console.log(number(nan)); //nan

console.log(number(true));  //1

console.log(number(false)); //0

console.log(number(undefined)); //nan
console.log(number(null));    //0
//數字字串

console.log(number("123")); //123

console.log(number("-123")); //-123

console.log(number("12.3"));

console.log(number("1e-7")); //1e-7

console.log(number("0xff")); //255

console.log(number("00123")); //123

console.log(number(" 123")); //123

console.log(number("\t\n123")); //123

//數字+字元或全字元字串

console.log(number('a123')); //nan

console.log(number("false")); //nan

console.log(number("a123")); //nan

//空串或者空格字串

console.log(number("\t\n")); //0

console.log(number(" ")); //0

console.log(number("")); //0

console.log(number({}));  //nan

console.log(number([1])); //1

console.log(number([1,2])); //nan

cosole.log(number()); //0

流程是:先string(x)變為字串,再將該值以y進製為基底轉為十進位制數,不填著為10,y的範圍: [2,36]

無引數y時:

console.log(parseint(123));   //123

console.log(parseint(1e-7)); //1

console.log(parseint(0xff)); //255

console.log(parseint(nan)); //nan

console.log(parseint(0.00001)); //0

console.log(parseint(true));  //nan

console.log(parseint(false)); //nan

console.log(parseint(undefined)); //nan

console.log(parseint(null)); //nan

console.log(parseint(""));   //nan

console.log(parseint("-123")); //-123

console.log(parseint(" ")); //nan

console.log(parseint("\t\n")); //nan

console.log(parseint("a123")); //nan

console.log(parseint("123a")); //123

console.log(parseint());   //"[object object]"->nan

console.log(parseint()); //""->nan

console.log(parseint([1,2])); //"1,2"->//1

console.log(parseint("f",2));   //nan

console.log(parseint("11f",2); //3

console.log(parseint("123",37)); //nan

console.log(parseint("0xff",0)); //255

console.log(parseint("0xff",nan)); //255

console.log(parseint("ff",)); //nan

console.log(parsefloat(0xff));  //255

console.log(parsefloat("0xff")); //0

console.log(parseint("0xff")); //255 個人猜測parseint(x,y)有y的存在所以能正確輸出

console.log(parsefloat(1e22)); //1e22

console.log(parsefloat(1e-7)); //1e-7

console.log(parsefloat("00123")); //123

console.log(parsefloat(" ")); //nan

console.log(string(null));  //"null"

console.log(0xff.tostring(2)); //"11111111"

console.log(oxff.tostring()); //"255" 不填預設10進製

JavaScript 資料型別

變數 宣告區域性變數 varmessage 宣告全部變數 message typeof 例如 var message some string alert typeof message string alert typeof message string alert typeof 95 number ...

Javascript資料型別

ecmascript中有5種簡單資料型別 也稱為基本資料型別 1.undefined 該型別只有乙個值,即undefined。在使用var宣告但未初始化時,這個變數就是undefined 2.null 該型別只有乙個值,即null。null undefined 3.boolean 4.number ...

JavaScript資料型別

undefined派生於null,因此在使用 進行比較時會返回true 沒有必要將變數值顯示宣告undefined 宣告空物件時應將其值賦值為nullundefined與null的關係 3 boolean true為真,false為假 true不一定 1,false不一定 0 使用boolean 進...