JavaScript의 변수와 타입
변수
어떤 값(데이터)을 담는 저장소(메모리)의 이름
1
let a = 1; // '='은 같다는 의미가 아니라 할당의 의미
타입
데이터의 자료형
1
2
3
4
5
6
7
let number = 1; // number(숫자)
let string = "1"; //string(문자)
let boolean = true; // boolean(참,거짓)
let undefined; //undefined(할당된 값이 없어 자료형이 정해지지 않은 상태)
undefined 와 비슷한 의미로 null 이 있지만 null은 변수는 존재하나 값이 null로 할당된 상태, 즉 자료형은 정해진 상태이다.
1
2
console.log(undefined == null); // true
console.log(undefined === null); // false
== 연산자는 자동 형변환이 가능하기 때문에 undefined 와 null을 비교해도 true가 나온다. === 연산자는 타입까지 비교 하기 때문에 false
자료형을 확인하는 typeof 메소드
1
2
3
4
5
6
7
8
9
10
11
12
13
let number = 1;
console.log(typeof number); // number
let string = "1"; //string
console.log(typeof string); // string
let boolean = true;
console.log(typeof boolean); // boolean
let undefined;
console.log(typeof undefined);
// Uncaught SyntaxError: Identifier 'undefined' has already been declared
// 잡히지 않은 구문 오류: 식별자 '정의되지 않음'이 이미 선언되었습니다.