取數字千分位

const number = 1000000
number.toLocaleString() // 1,000,000

建立時間戳

常用時間戳為1970年1月1日的毫秒數

+new Date()

字串轉數字

const string = '10000'
+string // 10000

數字加總

const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
eval(array.join('+')) // 55
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
array.reduce((a, b) => a + b) // 55

去除陣列重複項目

const array = ['apple', 'banana', 'banana', 'apple', 'banana']
[...new Set(array)] // ['apple', 'banana']

陣列內項目隨機交換

const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
array.sort(() => Math.random() - 0.5) // [3, 7, 9, 4, 2, 5, 6, 8, 10, 1]

處裡貨幣單位

(1e6).toLocaleString('zh-tw', { style: 'currency', currency: 'TWD' })
// $1,000,000.00

取陣列中最大值與最小值

const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Math.max(...array) // 10
Math.min(...array) // 1

建立連續數字陣列

Array(10).fill().map((i, id) => id + 1) 
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]