TIL - Javascript(헷갈리는 부분 정리 1) - Variable

홍예찬·2020년 8월 14일
0
post-thumbnail

(유튜브 드림코딩 by엘리 참고)
https://www.youtube.com/channel/UC_4u-bXaba7yrRz_6x6kb_w

1. 'use strict';

자바스크립트를 처음 실행할 때 써주는 게 좋음

2. block scope

{
let name = 'Tom'
console.log(name);
name = 'Tommy'
console.log(name);
}
console.log(name);  
//아무 값도 나오지 않음(block scope 안에 있는 값은 밖에서 찾을 수 없다.)

const count = 17;
const size = 17.1;
console.log(`value: ${count}, type: ${typeof count}`);
//value: 17, type: number
console.log(`value: ${size}, type: ${typeof size}`);
//value: 17.1, type: number

3. number

const infinity = 1 / 0;
const negativeInfinity = -1 / 0;
const nAn = 'not a number' / 0;
console.log(infinity);          //Infinity
console.log(negativeInfinity);  //-Infinity
console.log(nAn);               //NaN

4. symbol

const symbolOne = Symbol('id');
const symbolTwo = Symbol('id');
console.log(symbolOne === symbolTwo); //false
const hsymbolOne = Symbol.for('id');
const hsymbolTwo = Symbol.for('id');
console.log(hsymbolOne === hsymbolTwo);  //true
console.log(`value: ${symbolOne.description}, type: ${typeof symbolOne}`);  
//symbol을 나타내기 위해선 .description을 써야 한다.

5. Dynamic typing

let text = 'hello';
console.log(`value: ${text}, type: ${typeof text}`);
text = 1
console.log(`value: ${text}, type: ${typeof text}`);
text = '7' + 5
console.log(`value: ${text}, type: ${typeof text}`);  
//js에서는 string에 number를 더하면 string으로 type을 변환시킴
text = '8' / '2'
console.log(`value: ${text}, type: ${typeof text}`);  
//숫자들을 나눌 수 있는 나누기 연산자를 썼기 떄문에 string으로 인식하지 않고 number로 변환.
profile
내실 있는 프론트엔드 개발자가 되기 위해 오늘도 최선을 다하고 있습니다.

0개의 댓글