JAVASCRIPT

데이터 타입 확인 방법

funfunweb 2025. 5. 27. 18:30

✅ 1. 자바스크립트 데이터 타입 분류

🔹 원시(Primitive) 데이터 타입

  • string – 문자열 ("hello")
  • number – 숫자 (42, 3.14)
  • bigint – 큰 정수 (12345678901234567890n)
  • boolean – 참/거짓 (true, false)
  • undefined – 값이 정의되지 않음
  • null – 명시적 “없음”
  • symbol – 고유한 식별자

🔹 객체(Object) 데이터 타입

  • Object – 일반 객체 { key: value }
  • Array – 배열 [1, 2, 3]
  • Function – 함수 function() {}
  • Date, RegExp, Map, Set 등

✅ 2. 데이터 타입 확인 방법

① typeof – 기본 타입 확인

typeof 123;           // "number"
typeof "hello";       // "string"
typeof true;          // "boolean"
typeof undefined;     // "undefined"
typeof null;          // ❗ "object" ← 오래된 버그
typeof [];            // "object"
typeof function() {}; // "function"

② Array.isArray() – 배열 여부 확인

Array.isArray([]); // true
Array.isArray({}); // false

 

③ instanceof – 클래스 인스턴스 확인

 

[] instanceof Array;         // true
new Date() instanceof Date;  // true
{} instanceof Object;        // true

 

④ Object.prototype.toString.call() – 정확한 타입 확인

Object.prototype.toString.call(null);        // "[object Null]"
Object.prototype.toString.call(undefined);   // "[object Undefined]"
Object.prototype.toString.call([]);          // "[object Array]"
Object.prototype.toString.call("hi");        // "[object String]"
Object.prototype.toString.call(123);         // "[object Number]"
Object.prototype.toString.call(new Date());  // "[object Date]"

 

✅ 3. 예제 코드

 

function checkType(value) {
  console.log("typeof: ", typeof value);
  console.log("Array.isArray: ", Array.isArray(value));
  console.log("instanceof Object: ", value instanceof Object);
  console.log("Object.prototype.toString: ", Object.prototype.toString.call(value));
}

checkType([1, 2, 3]);

 

✅ 요약 비교

 

값                                                       typeof                                         정확한 타입 확인 (Object.prototype.toString.call())

 

null "object" "[object Null]"
[] "object" "[object Array]"
{} "object" "[object Object]"
123 "number" "[object Number]"
"hi" "string" "[object String]"
function() {} "function" "[object Function]"
 

✅ 예제: typeof + document.write() 사용

 

<!DOCTYPE html>
<html lang="ko">
<head>
  <meta charset="UTF-8">
  <title>typeof 출력 예제</title>
</head>
<body>

  <script>
    let num = 42;
    let str = "안녕하세요";
    let isTrue = true;
    let empty = null;
    let undef;
    let arr = [1, 2, 3];

    document.write("num 타입: " + typeof num + "<br>");        // number
    document.write("str 타입: " + typeof str + "<br>");        // string
    document.write("isTrue 타입: " + typeof isTrue + "<br>");  // boolean
    document.write("empty 타입: " + typeof empty + "<br>");    // object (주의!)
    document.write("undef 타입: " + typeof undef + "<br>");    // undefined
    document.write("arr 타입: " + typeof arr + "<br>");        // object (배열도 객체)

  </script>

</body>
</html>

✅ 출력 결과 (브라우저 화면에):

num 타입: number  
str 타입: string  
isTrue 타입: boolean  
empty 타입: object  
undef 타입: undefined  
arr 타입: object

 

⚠️ 주의할 점

  • typeof null → "object": 자바스크립트의 역사적인 버그입니다.
  • 배열도 typeof로는 "object"로 출력됩니다. 배열인지 정확히 확인하려면 Array.isArray()를 사용하세요.