註解使用的是 //
的方式
保留字
數字
數字方面,javascript的數字允許精確表達2^53 ~ 2^-53之間
Math,在ES6加入了更多定義上的函式
1 2
| console.log(Math.pow(2, 8)) console.log(Math.cbrt(8))
|
字串
1 2 3 4 5
| let s = "hello world";
console.log(s.length)
|
全域物件
1.常數
2.函式。p.s. isNaNA()
……等
3.建構器函式,Date()
, parseInt()
……等
4.Math, Json 之類的全域物件
不可變的原始值和可變的物件
1 2 3 4
| let s = "hello world";
console.log(s.toUpperCase()); console.log(s)
|
陣列
1 2 3 4 5 6 7 8 9 10 11
| let s = ["1", "2"]; let a = []; for (let i = 0; i < s.length; i++){ a[i] = s[i]; } console.log(a)
let c = Array.from(s); console.log(c);
|
型別轉換
1 2 3 4
| console.log(10+"x"); console.log("2"*"5"); let n = 1 - "x" console.log(n+"hello");
|
明確轉換
1 2 3 4 5 6
| console.log(Number("3")); console.log(String(false));
console.log(Boolean([])); console.log(parseInt("3 re")); console.log(parseFloat(" 3.5"));
|
Let, Const
1 2 3 4 5 6
| let i; let j, sum;
const a = 5;
|
務必避免重複宣告
1 2 3 4 5 6
| const x = 5; if (x == 5){ let x = 2; console.log(x); } console.log(x);
|
解構指定(右手邊給左手邊)
1 2 3 4 5 6 7
| let [x, y] = [1]; let [x, y] = [1, 2, 3]
let a = {x:1, y:2}; for(const[name, value] of Object.entries(a)){ console.log(`${name} : ${value}`); }
|