- JS는 한마디로 동적 타입 언어(dynamic/week type)
- 선언이 아닌 할당에 의해 타입이 결정(타입추론/type inference)
- 값을 할당하는 시점에 변수의 타입이 동적으로 결정됨
- 재할당에 의해 변수의 타입은 언제든 동적으로 변할 수 있음
| 구분 | 데이터 타입 | 설명 |
|---|
| 숫자 | number 타입 | 숫자, 정수와 실수 구분 없이 하나의 숫자 타입만 존재 |
| 문자열 | string 타입 | 문자열 |
| 불리언 | boolean 타입 | 논리적 참(true)과 거짓(false) |
| undefined | undefined 타입 | var 키워드로 선언된 변수에 아무값도 할당되지 않는 경우 |
| null | null 타입 | 값이 없다는 것을 의도적으로 명시할 때 사용하는 값 |
| 심볼 | symbol 타입 | ES6에서 추가된 7번째 타입 |
| 객체 | object 타입 | 객체, 함수, 배열 등 |
number type
var integer = 10;
var double = 10.12;
var negative = - 2 0 ;
var binary = 0b01000001;
var o c t a l = 00101;
var hex = 0x41;
console.log(binary);
console.log(octal);
console.log(hex);
console.log(binary === octal);
console.log(octal === hex);
string type
var string;
string = '문자열' ;
string = "문자열";
string = `문자열` ;
var num1 = 1;
var num2 = 2;
var result = 3;
var string1 = num1 + ' 더하기 ' + num2 + ' 는 \ ' ' + result + '\' ';
Template literal
- 줄바꿈 및 공백은 이스케이프 시퀀스 or ``(백틱)으로 표현
- 멀티라인 문자열(multi-line string), 표현식 삽입(expression interpolation), 태그드 템플릿(tagged template) 제공
var template = '<ul>\n\t<li><a href="#">Home</a></li>\n</ul>';
var template = `‹ul›
〈Li><a href="#">Homes/a></li)
</ul>`;
var num3 = 1;
var num4 = 2;
var result2 = 3;
var string1 = `${num1} 더하기 ${num4}는 ${result2}`;
boolean
var foo = true;
console. log(foo);
foo = false;
console. log(foo);
undefined
null
symbol
- ES6에서 추가됨
- 다른 값과 중복되지 않는 유일무이한 값
var key = Symbol('key');
console.log(typeof key);
var obj = {};
obj[key] = 'value';
console. log(obj[key]);