# 데이터타입, data types, let vs var, hoisting

**입력 연산 출력 -> CPU 최적화된 연산 + 메모리 사용 최소화가 중요하다.**

### ​ 변수 (variable)

**변경될 수 있는 값 , 빈데이터에 변수를 선언 하고 값을 할당함**

![](https://phinf.pstatic.net/memo/20200829_185/1598667653629XOmRS_PNG/1598667651186.png?type=w740)

### let

* 변수 변경될 수 있는 값을 선언할때 사용 (added in ES6)&#x20;
* 읽기와 쓰기가 다 가능하다.
* MUTABLE **타입이다. (변경가능)**

### block scope {}

코드를 블록 스콥해주면 블록스콥 안에서만 작동하게 된다. ​

* 반대는 {} 의 바깥 부분에서 선언된 globalname = 'gobal name'; 글로벌변수
* &#x20;전역변수는 어느곳이던 접근이 가능하다.
* &#x20;그리고 블록스콥 안에서도 인지하고 있음 그렇기떄문에 전역변수는 최소한으로 사용하는것이 좋고, 가능한 필요한곳에만 사용하는것이 좋다.

### var 단점

var은 값을 선언하기도 전에 쓸 수 있는 치명적인 단점이 있다.&#x20;

![](https://810537134-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MZh1fxUzW3zjggiSfv4%2F-M_pxMOFmLUhnnBsR_9J%2F-M_pxbqGZeS5_2GQhH4m%2Fimage.png?alt=media\&token=dc438f86-db34-4a54-9319-e4c4e16176b5)

* var은 블록스콥{} 을 철저히 무시함 -> 안에서 선언해도 출력이됨.
* var hoisting -> 어디에 선언 했는지 상관없이 항상 제일 위로 끌어올려주는것을 의미한다.

### constants( const )

![](https://phinf.pstatic.net/memo/20200828_7/1598590886390AVxUc_PNG/1598590886343.png?type=w740)

* IMMUTABLE 변경 불가능함 한번 할당하면 값이 절대로 바뀌지 않음&#x20;
* 보안상 상당히 좋음 , 변경될것이 없다면 const를 사용하는것이 좋음&#x20;
* 읽기만 가능하다 다시 다른값으로 쓰는것은 불가능 하다.

### variable types​

![](https://810537134-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MZh1fxUzW3zjggiSfv4%2F-M_py_OBM0Vm9DTTVbg1%2F-M_pyk9OIqGItkPS9FTg%2Fimage.png?alt=media\&token=7718376e-e353-46af-a4ae-055cfd222c26)

* primitive, single item: number, string, boolean, null, undefiedn, symbol
* object, box  container
* function, frist-class function

### 다른언어와 자바스크립트의 차이점

![](https://phinf.pstatic.net/memo/20200828_274/1598591344889k1DPd_PNG/1598591344815.png?type=w740)

* c언어, java 에서는 숫자 변수 하나도 구체화 시켜서 데이터 관리를 할  수 있다.
* short, int는 작은 단위의 데이타 long 은 큰 데이타를 관리할때)​
* short, int long은 정수 / float, double은 소수점 단위를 사용할때 사용&#x20;

### bigInt&#x20;

![](https://phinf.pstatic.net/memo/20200828_134/15985920212542BlwU_PNG/1598592021212.png?type=w740)

* 숫자 끝에 n 만 붙여주면 bigInt로 간주되어짐&#x20;
* 크롬이랑 파이어폭스에서만 사용 가능함&#x20;

### string

![](https://810537134-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MZh1fxUzW3zjggiSfv4%2F-M_pz6TsPLfmNZhG2V80%2F-M_pzxqndAdiGVd0tBNw%2Fimage.png?alt=media\&token=6f9c639c-acaf-41a0-bab6-728751c65c6b)

###

### null undefined 의 차이

```javascript
//null
let notinign = null; // 내가 명확하게 빈값을 지정하는것 
//undefined 
let x = undefined; // 선언은 되었지만 값이 비어있는 상태
```

### symbol

**주어진 식별자와 상관없이 고유한 값을 만들때 사용한다.**

```javascript
// symbol 
const symbol1 = symbol('id'); // 똑같이 쓰고 싶다면 symbol.for을 써주면된다.
const symbol2 = symbol('id');
console.log(symbol1 === symbol2); 

console.log(`value: ${symbol1.description}`); // symbol은 바로 출력이 안됨 .description 을 꼭 붙여줘야함
```

###

### Dynamic typing: dynamically typed language

```javascript
let text ='hello';
console.log(`value: ${text}, type: ${tyoe0f text}`); // value: hello, type: string
text = 1;
console.log(`value: ${text}, type: ${tyoe0f text}`); // value:1, type: number
text = '7' + 5;
console.log(`value: ${text}, type: ${tyoe0f text}`); // value: 75, type: string
text = '8' + '2';
console.log(`value: ${text}, type: ${tyoe0f text}`); // value:4, type: number
```

**이렇게 예측 못하는 상황이 많기때문에 -> 타입스크립트가 나옴**&#x20;

### primitive, object ​​차이점

![](https://810537134-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MZh1fxUzW3zjggiSfv4%2F-M_q2C2TRlu9cNNCr-yn%2F-M_q2iTn5GHlSBOv9ctH%2Fimage.png?alt=media\&token=86f0a047-eabf-4d90-a41f-6632472770fa)

* primitive는 value 값자체가 메모리에 저장된다. &#x20;
* object는 용량이 너무 커서 바로 저장 할 수 가없다.&#x20;
* const 로는 변경이 불가능하지만 const의 변수를 object화 하면 object속성이기때문에 변경이 가능하다.
