# javascript ES6 資料型別 [Notion-page](https://yinchen.notion.site/javascript-ES6-3658fb71232e41c492f8644645de5552) # 動態型別 ## 型別確認 使用 `typeof` 程式 : `console.log(typeof yourVariable)` 表示在執行階段才會確立變數的型別,會根據賦值後決定型別,如下例 ```jsx let num = 1; console.log(typeof num); // 這邊是 number num = "1"; console.log(typeof num); // 現在為 string ``` ## 顯性轉換 Explicit Conversion 表示當一個變數先訂好值,而後再賦予另一種型別的值,如上例 ## 隱性轉換 Implicit Conversion 不清楚一些規則會出現與預想不合的情況,可以說是特例情況 ```jsx let num = 1; console.log(num, typeof num); // number num = num + ""; console.log(num, typeof num); // string num = num * 3; console.log(num, typeof num); // number ``` `NaN` 在 JS 裡是 `number` 型別 ```jsx let test = NaN; console.log(test, typeof test); ``` # 兩大型別 只要記得原始型別有哪些,剩下的都是物件型別 ## 原始型別 - **Boolean** - **Null : 他是 Object 型別,算一種特殊案例** - **Undefined : 預設沒有宣告的變數會出現此型別** - **Number** - **String** - Biglnt - Symbol ## 原始型別包裹物件 - new Boolean() - new Number() - new String() - Biglnt() - Symbol() 原始型別具有一些特定的 function 使用,例如 : 字串在 `string()` 裡具有大小寫轉換的方法可以用,要查詢原始型別具有哪些方法可以用,需要透過包裹物件查看 ```jsx let a = "test "; console.log(a.length); console.log(a.toUpperCase()); console.log(a.trim()); let b = new String(a); // 這個又稱為 建構式 console.log(b); ```  一般在原始型別不要使用建構式去創建,這是針對物件型別的作法 要看包裹物件的結果無法使用 codepen 顯示,需要用網頁上的檢查 ## 物件型別 建構式的宣告方式就是物件型別 # 作業練習 [https://codepen.io/yinchengchen/pen/VwbQbEm?editors=1111](https://codepen.io/yinchengchen/pen/VwbQbEm?editors=1111)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up