將字符串轉換爲數字 var iNum1 = parseInt(「1234www.itxueyuan.com」); //結果爲 1234 var iNum2 = parseInt(「0xA」); //結果爲 10 var iNum3 = parseInt(「22.5」); //結果爲 22 var iNum4 = parseInt(「blue」); //結果爲 NaN
var iNum1 = parseInt(「AF」, 16); //結果爲 175 var iNum1 = parseInt(「10」, 2); //結果爲 2 var iNum2 = parseInt(「10」, 8); //結果爲 8 var iNum2 = parseInt(「10」, 10); //結果爲 10
var fNum1 = parseFloat(「1234blue」); //結果爲 1234.0 var fNum2 = parseFloat(「0xA」); //結果爲 NaN var fNum3 = parseFloat(「22.5」); //結果爲 22.5 var fNum4 = parseFloat(「22.34.5」); //結果爲 22.34 var fNum5 = parseFloat(「0908」); //結果爲 908 var fNum6 = parseFloat(「blue」); //結果爲 NaN
4、類型轉換
var b1 = Boolean(「」); //false – empty string var b2 = Boolean(「hi」); //true – non-empty string var b3 = Boolean(100); //true – non-zero number var b4 = Boolean(null); //false - null var b5 = Boolean(0); //false - zero var b6 = Boolean(new Object()); //true – object
Number(false) 0 Number(true) 1 Number(undefined) NaN Number(null) 0 Number(「5.5」) 5.5 Number(「56」) 56 Number(「5.6.7」) NaN Number(new Object()) NaN Number(100) 100