[Javascript] 데이터 타입

한별·2024년 3월 17일

Javascript

목록 보기
2/25

원시 타입

: 객체를 제외한 모든 타입
: 메서드를 갖지 않음

null

: 비어있는 값을 표현

⚠️ 주의
typeofnull을 확인했을 때 'object'라는 결과가 반환됨. (언어 자체 오류)

typeof null === 'object' // true

undefined

: 값이 할당되지 않은 변수/인수

boolean

: 참/거짓

number

: -(253-1) ~ (253-1) 사이의 모든 숫자

Math.pow(2, 53) - 1 === Number.MAX_SAFE_INTEGER // true
-(Math.pow(2, 53) - 1) === Number.MIN_SAFE_INTEGER // true
9007199254740992 === 9007199254740993 // 다른데 true

지수

let num = 2.5e5;
console.log(num); // 250000
console.log(typeof num); // number

NaN

let num = 'Hello' / 2;
console.log(num); // NaN
console.log(typeof num); // number

Infinity

let num = 1 / 0;
console.log(num); // Infinity
console.log(typeof num); // number

let num = -1 / 0;
console.log(num); // -Infinity
console.log(typeof num); // number

bigint

: number가 다룰 수 있는 숫자 크기의 제한을 극복, 더 큰 숫자를 저장할 수 있게 해줌.
: 숫자 뒤에 n을 붙이거나 BigInt()를 사용하여 생성

9007199254740992n === BigInt(9007199254740993) // true
9007199254740992 == 9007199254740992n // true
9007199254740992 === 9007199254740992n // false

string

: 문자열
: '', "", ``로 감싸져있다.

백틱으로 작성한 문자열: 템플릿 리터럴

  • 줄바꿈이 가능
  • 문자열 내부에 표현식을 쓸 수 있다

symbol

: 중복되지 않는 어떠한 고유한 값을 나타내기 위해 사용
: Symbol()을 사용하여 생성
: 동일한 값을 사용하기 위해서는 Symbol.for을 사용

Symbol('key') === Symbol('key') // false
Symbol.for('hello') === Symbol.for('hello') // true

객체 타입

object

typeof [] // 'object'
typeof {} // 'object'
function fn () {}
typeof fn // 'function'

+) falsy

boolean 타입인 false가 아니라도 조건문에서 false로 취급되는 값

타입
falseboolean
nullnull
undefinedundefined
NaNNumber
0, -0, 0n, 0x0nNumber, BigInt
'', "", ``String

참고 자료

모던 리액트 Deep Dive

profile
글 잘 쓰고 싶어요

0개의 댓글