TIL [데이터 타입]

데이터 타입이란?

변수에 저장되는 데이터의 유형으로 Primitive(원시) 데이터 타입(string, number, boolean, undefined, null, symbol 등)과 Object 데이터 타입(function, object, array 등)이 있다.

1. 문자열(string)

' ' 나 " "을 써서 만든 데이터 타입

let str1 = '문자열';

문자 안에 문자가 들어갈 경우

let str1 = '문자는 "문자"';
console.log(str1); // 문자는 "문자"

2. 숫자(number)

정수, 소수점, 지수를 표현할 수 있는 데이터 타입

let num1 = 3;
let num2 = 10.5;
let num3 = le+2; // 100

내림(floor)

var num = 10.1;
Math.floor(num);
console.log(num); // 10

3. boolean(논리)

true(참)와 false(거짓)의 값을 표현하는 데이터 타입

let isBoolean1 = (1 < 2);
let isBoolean2 = (1 > 2);
console.log(isBoolean1); // true 
console.log(isBoolean2); // false

6가지 falsy값

false, null, undefined, 0, NaN, ''

코플릿 [조건문 #17]

function isFalsy(anything){
  return !Boolean(anything); // anything이 falsy값이면 true반환
}

4. typeof 명령

let num = 3;
let str = 'hello';
console.log(typeof num); // number
console.log(typeof str);// string

한 가지 주의 사항으로 typeof array는 'array'가 아니라 'object'이다. Array일때는 Array.isArray()을 쓰자!

좋은 웹페이지 즐겨찾기