
데이터 타입
데이터 타입은 변수에 저장되는 데이터의 유형으로 원시(Primitive)데이터 타입 과 객체(Object)데이터 타입 으로 나눌 수 있습니다.
원시(Primitive)데이터 타입으로는 number, strong, boolean, undefined, null, symbol 등이 있으며, 객체(Object)데이터 타입에는 function, object, array 등이 있습니다.
number(숫자) 데이터
number 데이터는 정수, 소수점, 지수를 표현할 수 있습니다.
var num2 = 10.5;
var num3 = le+2;
console.log(num1); // 10
console.log(num1); // 10
console.log(num3); // 100 ,le+2는 1*10의 2승을 의미합니다.
string(문자) 데이터
string 데이터는 '' 또는 "" 으로 표현할 수 있습니다.
var str2 = "문자";
conaole.log(str1); // 문자
conaole.log(str2); // 문자
문자 안에 문자가 들어 갈 경우
var str2 = "문자 '문자'";
conaole.log(str1); // 문자 "문자"
conaole.log(str2); // 문자 '문자'
boolean(논리) 데이터
boolean 데이터는 true(참) 와 false(거짓)의 값으 표현합니다.
var temp2 = (5 < 4);
console.log(temp1); //true
console.log(temp2); //false
boolean에서 0은 false를 의미하며, 0이외의 값은 true를 의미합니다.
var temp2 = 1; //1의 boolean은 true를 의미합니다.
console.log(temp1); //false
console.log(temp2); //true
undefined 데이터
var temp2 = ;
console.log(temp1); //10
console.log(temp2); //undefined, 변수에 데이터 값이 없기 때문
obj.name = '홍길동';
obj.age;
console.log(obj.name); //홍길동
console.log(obj.age); //undefined, 객체에 속성 값이 없기 때문
null 데이터
null 데이터는 undefined와 유사하지만, 데이터를 저장하였으나 값이 존재 하지 않을 때 null값을 반환홥니다.
obj = null;
console.log(obj.name); //null
var obj = document.getElementById('gnb');
console.log(obj); //변수에 데이터 값을 저장은 하였으나 값이 존재하지 않을 경우
undefined와 null의 boolean은 false 입니다.
var obj2 = null; // null
console.log(Boolean(obj1)); //false
console.log(Boolean(obj2)); //false
typeof 명령
변수에 저장 되어 있는 데이터의 타입을 알아보려면 typeof를 사용합니다.
var str = '문자';
console.log(typeof num); //number
console.log(typeof str); //string
'JavaScript' 카테고리의 다른 글
지역변수와 전역변수 (4) | 2022.07.28 |
---|---|
함수에 대해 알아보기 (8) | 2022.07.26 |
If문 (6) | 2022.07.26 |
for문 이해하기 (12) | 2022.07.21 |
연산자 (10) | 2022.07.20 |
댓글