---
# System prepended metadata

title: '[筆記] JS老是忘記的東東'
tags: [JAVASCRIPT]

---

---
tags: JAVASCRIPT
---
# [筆記] JS老是忘記的東東

## 7 資料型態
`primitive type`
1. null
2. undefined
3. string
4. number
5. boolean
6. symbol(ES6)

`其它的都是object，可以被改變`

7. object

## primitive type is immutable
```javascript=
const str = 'GoGo'
str.toLowerCase()
console.log(str)
// GoGo
```
- Array可變
```javascript=
const arr = [1, 2]
arr.push(3)
console.log(arr)
// [1, 2, 3]
```

```javascript
// bug
console.log(typeof null)
```
- 確認是否為array
```javascript=
Array.isArray(arr)
```
- 確認是否為function
```javascript=
typeof funct === 'function'
```
- 準確判斷型態(支援舊瀏覽器)
```javascript=
// [object Number]
Object.prototype.toString.call(num)
// [object Array]
Object.prototype.toString.call(arr)
// [object Function]
Object.prototype.toString.call(funct)
```
- 防未宣告
```javascript=
if(typeof window !== 'undefined') {
    
}
```

## 2個array比對
```javascript=
const a = [1, 2, { age: 12 }, [3], [[8]]]
const b = [1, 2, { age: 12 }, [3], [[8]]]
a.every((element, i) => element === b[i])
// false
JSON.stringify(a) === JSON.stringify(b)
// true
```