本篇會談到
型別可以分為兩大類:
number
數字數字型別第一個數字不會有 0
let a = 0912345678;
console.log(a);
// 912345678
// 注意:數字型別沒有前置零
let b = 0.912345678;
console.log(b);
// 0.912345678
let c = 0;
console.log(c);
// 0
Symbol
獨一無二的值ES6 新的一種基本資料型態
Symbol 值透過 Symbol() 函數來生成
let s1 = Symbol();
let s2 = Symbol();
s1 === s2;
// false
// 因為是獨一無二的
Symbol() 是一個函數,不能用 new
去建立
let s = new Symbol();
// TypeError: Symbol is not a constructor
typeof
檢測資料型別使用 typeof 查詢資料型別,所回傳的值會是字串型別
利用 typeof 來看是什麼型別!
string
boolean
number
object
undefined
function
null
是 (object),JS 在剛開始就是設定這個 type[1, 2, 3]
是 (object),在JS裡面 type 沒有 Array
typeof 'Hello World!';
// 'string' 字串
typeof true;
// 'boolean' 布林
typeof 1234567;
// 'number' 數字
typeof null;
// 'object' 物件
typeof undefined;
// 'undefined'
typeof { name: 'Jack' }; // 'object'
typeof Symbol(); // 'symbol'
typeof function() {}; // 'function'
typeof [1, 2, 3]; // 'object'
typeof NaN;
// 'number' 數字
// NaN 是無效的數字,所以用 typeof 來看是數字型別
// NaN 與任何數字運算都會得到 NaN,並且 NaN 不大於、不小於也不等於任何數字,也不包含 NaN 本身
parseInt()
字串轉數字parseInt(string,radix)
string
radix
let a = "123";
a = parseInt(a);
console.log(a)// 回傳 123
parseInt("1A34") // 回傳 1 ,A 無法被解析為數字,所以停止解析
toString()
數字轉字串
let a = 091234567;
a = a.toString();
console.log(a)
// "91234567"
typeof a;
// string
Number()
針對各類型數字進行轉換JS