JavaScript - Data Types

김두비·2022년 5월 10일

JavaScript입문

목록 보기
6/10

자료형

기본적으로 자바스크립트 자료형은 동적 타이핑이라고한다

let whatever = 'dubi'; //dubi라고 선언하고나면 문자열타입을 가진 변수가 된다

27 추가 시 숫자형 타입으로 변경됨

let whatever = 'dubi'; 

whatever = 27; //숫자형 타입으로 변경된다

자바스크립트는 변수가 가진 타입에 따라 값이 달라진다
정해져있지 않은 이런 타입을 동적 타이핑이라고한다

변수가 가지는 고정 타입이 없다, 하지만 타입이 없는 것은 아니다

데이터 타입 // ES6 기준

기본 타입 (Primitive Values)

  • Boolean
  • Null
  • Undefined
  • Number
  • String
  • Symbol

객체 (Objects)
브라우저에서 제공해주는 객체들이 있으며 표준내장객체라고함
표준내장객채로 또다른 객체를 만들 수 있음

Boolean

true / false 가 있으며 참, 거짓을 뜻한다
isTrue는 true라는 값을 가지게 되며 Boolean 타입을 가지게 된다

const isTrue = true;
const isFalse = false;

console.log(isTrue, typeof isTrue);
console.log(isFalse, typeof isFalse);

Boolean도 표준내장객체이며, Boolean을 이용해서 new를 지정하고나면 새로운 객체가 생성된다

const b = Boolean(false) //함수실행

console.log(b, typeof b);

값은 fase "boolean"이 찍히는데

Null

// Null

const a = null;

console.log(a, typeof a);

값이 없다라는 의미로 null이 나오게된다

Undefined

// Undefind

let b;

console.log(b, typeof b);


값이 undefined가 나오게되며 타입도 undefined가 나온다

null 같은 경우는 값은 가지고 있지만 비어있는 값이므로 null이 나오며 undefined는 값을 가지고 있지 않음 두 타입은 값이 제대로 정해져있지 않은 느낌

비교 해보기

// Null

const a = null;

console.log(a, typeof a);

// Undefind

let b;

console.log(b, typeof b);

if (a == b) {
  console.log(a == b);
}

값은 true가 나오게된다

정확하게 구분하기 위해 ===을 사용한다

// Null

const a = null;

console.log(a, typeof a);

// Undefind

let b;

console.log(b, typeof b);

if (a === b) {
  console.log(a === b);
}

Number

const a = 27; //a는 27, number이라는 타입을 가진다
const a = 27;

console.log(a, typeof a);

소수점도 똑같이 찍힌다

const b = 96.27;

console.log(b, typeof b);

const c = NaN;

console.log(c, typeof c);


number 숫자형 타입은 찍히곤 있지만 언제 쓰이는지 알 수 없다

const d = Number('dubi'); //문자열바꾸기, 문자열을 바꿧을 때 형변환이 된다

console.log(d, typeof d)

String

const a = 'dubi'; //따옴표를 사용해서 막아주고 사용하면 문자열이 됨 
console.log(a, typeof a);

더하기를 사용해서 다른 문자열과도 합칠 수 있다

const b = 'dubi' + 'kim';
console.log(b, typeof b);

다른 변수를 이용해서 합칠 수도 있다

const a = 'dubi'; //따옴표를 사용해서 막아주고 사용하면 문자열이 됨 
console.log(a, typeof a);

const c = a + 'kim'
console.log(c, typeof c);

여러가지 문자열을 합쳐야 할 때가 있다
ES6 문법 중 템플릿 스트링을 이용

const a = 'dubi';
console.log(a, typeof a);

const d = `${a} kim`;
console.log(d, typeof d);

Symbol

함수를 호출하는 용도로 사용

const a = Symbol();

c와 d가 서로 다른 값이 찍히게 된다
Symbol은 고유한 값을 추려낼 때 사용된다

const a = Symbol();
const b = Symbol(27);
const c = Symbol('dubi');
const d = Symbol('dubi');

console.log(a, typeof a);
console.log(c === d);

만들어진 심볼은 고유한 값을 의미한다
고유하게 처리할 것이 있으면 Symbol을 사용

profile
관심과 격려가 필요한 응애 개발자

0개의 댓글