DAY 6
0
Modern Web

## 型別轉換

JavaScript是弱型別的程式語言，對型別的要求並沒有像JAVA、C#如此嚴謹，相對地，彈性許多。

``````console.log(1 + 'ABC'); //1ABC
console.log('5' * '6'); //30
var x = 2 * 'a';
console.log(x); //NaN
``````

#### 型別轉換對照表

String Number Boolean Object
undefined "undefined" NaN false throws TypeError
null "null" 0 false throws TypeError
true "true" 1 new Boolean(true)
false "false" 0 new Boolean(false)
""(空字串) 0 false new String("")
"1.2" 1.2 true new String("1.2")
"one" NaN true new String("one")
0 "0" false new Number(0)
-0 "0" false new Number(-0)
NaN "NaN" false new Number(NaN)
Infinity "Infinity" true new Number(Infinity)
-Infinity "-Infinity" true new Number(-Infinity)
1 "1" true new Number(1)
{ } true
[] "" 0 true
[9] "9" 9 true
['a'] 使用join() NaN true
function(){} NaN true

#### 相等運算

JavaScript有兩種相等比較運算方式，一種是寬鬆(==)，一種是嚴格(===)。

JavaScript會自行把運算元轉換成它想要的型別，再進行比較。

null跟undefined雖然可以轉型成false，但它們並不等於false。

``````if(undefined){}
``````

#### 強制轉型

``````var x = 10;
console.log(typeof x); //number
var y = x + '';
console.log(typeof y); //string
console.log(typeof (+y)); //number
console.log(typeof (!!y)); //boolean
``````

x(number)加上空字串，會轉成x(string)。
y(string)前面加上+運算子，會轉成y(number)。
!!會先將y轉為否定的boolen值，再將boolen值反轉一次。

#### Number

toFixed( )

``````var x = 12345.6789;
console.log(x.toFixed()); //12346
console.log(x.toFixed(2)); //12345.68
console.log(x.toFixed(5)); //12345.67890
``````

Number( )

``````console.log(Number('')); //0
console.log(Number('123')); //123
console.log(Number('123.456')); //123.456
console.log(Number('ABC')); //NaN
console.log(Number(false)); //0
console.log(Number(true)); //1
console.log(Number(null)); //0
console.log(Number(undefined)); //NaN
``````

parseFloat( )

``````console.log(Number('123abc')); //NaN
console.log(parseFloat('123abc')); //123
console.log(Number('')); //0
console.log(parseFloat('')); //NaN
console.log(Number(true)); //1
console.log(parseFloat(true)); //NaN
console.log(Number(null)); //0
console.log(parseFloat(null)); //NaN
``````

parseInt( )

``````console.log(parseInt('123abc')); //123
console.log(parseInt('123.999')); //123
console.log(parseInt('0.1')); //0
console.log(parseInt('.1')); //NaN
console.log(parseInt('\$13.58')); //NaN
``````

#### 物件型別的轉換

JavaScript中的任何物件(陣列與函式)轉成Boolean值都是true。

``````var obj = {
x: 1,
y: 2
};
var ary = [1, 2, 3];

function f(x) {
return x;
}
console.log(obj.toString());
console.log(ary.toString());
console.log(f.toString());
``````

``````var obj = {
x: 1,
y: 2
};
console.log(obj.valueOf());
``````

``````var num1 = new Number(100)
var num2 = 100;
var str1 = new String('ABC');
var str2 = 'ABC';
var boo1 = new Boolean(true);
var boo2 = true;
console.log(num1.valueOf()); //100
console.log(typeof (num1)); //object
console.log(num2.valueOf()); //100
console.log(typeof (num2)); //number
console.log(str1.valueOf()); //ABC
console.log(typeof (str1)); //object
console.log(str2.valueOf()); //ABC
console.log(typeof (str2)); //string
console.log(boo1.valueOf()); //true
console.log(typeof (boo1)); //object
console.log(boo2.valueOf()); //true
console.log(typeof (boo2)); //boolean
``````

JavaScript大全
Speaking JavaScript｜簡明完整的 JS 精要指南

JavaScript Note31