typeof
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since â¨2015ë 7ìâ©.
typeof ì°ì°ìë í¼ì°ì°ìì íê° ì ìë£íì ëíë´ë 문ìì´ì ë°íí©ëë¤.
ìëí´ ë³´ê¸°
console.log(typeof 42);
// Expected output: "number"
console.log(typeof "blubber");
// Expected output: "string"
console.log(typeof true);
// Expected output: "boolean"
console.log(typeof undeclaredVariable);
// Expected output: "undefined"
구문
typeof operand
매ê°ë³ì
ì¤ëª
typeofê° ë°íí ì ìë ê°ì ìë íìì ë³¼ ì ììµëë¤. ìë£íê³¼ ììê°ì ëí ìì¸í ì ë³´ë JavaScript ìë£íê³¼ ìë£êµ¬ì¡° íì´ì§ë¥¼ ì°¸ê³ íì¸ì.
| Type | Result |
|---|---|
| Undefined | "undefined" |
| Null | "object" (ìë ì°¸ê³ ) |
| Boolean | "boolean" |
| Number | "number" |
| BigInt | "bigint" |
| String | "string" |
| Symbol | "symbol" |
| Function ê°ì²´ (ECMA-262 ííì¼ë¡ë [[Call]]ì 구ííë ê°ì²´) | "function" |
| ë¤ë¥¸ 모ë ê°ì²´ | "object" |
ì´ ëª©ë¡ì ê°ë¥í 모ë ê°ì í¬í¨í©ëë¤. ëª ì¸ë¥¼ ì¤ìíë ìì§ìì ì´ì¸ì ê°ì´ ìì±ë ì¬ë¡ë íì¬ê¹ì§ë ë³´ê³ ëì§ ìììµëë¤.
ìì
>기본 ì¬ì©ë²
// Numbers
typeof 37 === "number";
typeof 3.14 === "number";
typeof 42 === "number";
typeof Math.LN2 === "number";
typeof Infinity === "number";
typeof NaN === "number"; // "ì«ìê° ìë(Not-A-Number)" ììë ë¶êµ¬íê³
typeof Number("1") === "number"; // Numberë 문ìì´ì ì«ìë¡ ë³ííë ¤ ìëí¨
typeof Number("shoe") === "number"; // ì«ìë¡ ë³íí ì ìë ê°ë í¬í¨ë¨
typeof 42n === "bigint";
// Strings
typeof "" === "string";
typeof "bla" === "string";
typeof `template literal` === "string";
typeof "1" === "string"; // 문ìì´ ìì ì«ìê° ìì´ë typeof ê²°ê³¼ë string
typeof typeof 1 === "string"; // typeofë íì 문ìì´ì ë°íí¨
typeof String(1) === "string"; // Stringì ì´ë¤ ê°ì´ë 문ìì´ë¡ ë³í, toString()ë³´ë¤ ìì í¨
// Booleans
typeof true === "boolean";
typeof false === "boolean";
typeof Boolean(1) === "boolean"; // Boolean()ì ê°ì truthy/falsy ì¬ë¶ì ë°ë¼ ë³íí¨
typeof !!1 === "boolean"; // ! (ë
¼ë¦¬ ë¶ì ) ì°ì°ì를 ë ë² ì ì©íë ê²ì Boolean()ê³¼ ëì¼í¨
// Symbols
typeof Symbol() === "symbol";
typeof Symbol("foo") === "symbol";
typeof Symbol.iterator === "symbol";
// Undefined
typeof undefined === "undefined";
typeof declaredButUndefinedVariable === "undefined";
typeof undeclaredVariable === "undefined";
// Objects
typeof { a: 1 } === "object";
// ë°°ì´ê³¼ ì¼ë° ê°ì²´ë¥¼ 구ë¶íë ¤ë©´ Array.isArray ëë Object.prototype.toString.call ì¬ì©
typeof [1, 2, 4] === "object";
typeof new Date() === "object";
typeof /regex/ === "object";
// ë¤ìê³¼ ê°ì ì½ëë í¼ëì¤ë½ê³ , ìííë©°, ëë¹ì ì´ë¯ë¡ í¼íì¸ì.
typeof new Boolean(true) === "object";
typeof new Number(1) === "object";
typeof new String("abc") === "object";
// Functions
typeof function () {} === "function";
typeof class C {} === "function";
typeof Math.sin === "function";
typeof null
// ì´ë JavaScriptê° ì²ì ìê²¼ì ëë¶í° ì¡´ì¬í´ ììµëë¤
typeof null === "object";
JavaScript를 ì²ì 구íí ë, JavaScript ê°ì íì
íê·¸ì ê°ì¼ë¡ íìëììµëë¤. ê°ì²´ì íì
íê·¸ë 0ì´ììµëë¤. nullì Null pointer(ëë¶ë¶ì íë«í¼ìì 0x00)ë¡ íìëììµëë¤. ê·¸ ê²°ê³¼ nullì íì
íê·¸ë¡ 0ì ê°ì§ë©°, ë°ë¼ì typeofë object를 ë°íí©ëë¤. (ì°¸ê³ ë¬¸ì)
ECMAScriptì ìì ì´ ì ì(opt-inì íµí´)ëìì¼ë ê±°ì ëììµëë¤. ì ìë ê²ì ë¤ìê³¼ ê°ìµëë¤. typeof null === 'null'.
new ì°ì°ì ì¬ì©í기
newì í¨ê» í¸ì¶ë 모ë ìì±ì í¨ìë ë¹ììê°("object" ëë "function")ì ë°íí©ëë¤. ëë¶ë¶ì ê°ì²´ë¥¼ ë°ííì§ë§, ìì¸ì ì¼ë¡ Functionì í¨ì를 ë°íí©ëë¤.
const str = new String("String");
const num = new Number(100);
typeof str; // "object"
typeof num; // "object"
const func = new Function();
typeof func; // "function"
구문ìì ê´í¸ì íìì±
typeof ì°ì°ìë ë§ì
(+) ê°ì ì´í ì°ì°ìë³´ë¤ ë ëì ì°ì°ì ì°ì ìì
를 ê°ì§ëë¤. ë°ë¼ì ë§ì
ê²°ê³¼ì íì
ì íê°íë ¤ë©´ ê´í¸ê° íìí©ëë¤.
// ííìì ë°ì´í° íì
ì ê²°ì í기 ìí´ ê´í¸ë¥¼ ì¬ì©í ì ììµëë¤.
const someData = 99;
typeof someData + " foo"; // "number foo"
typeof (someData + " foo"); // "string"
ì ì¸ëì§ ììê±°ë ì´ê¸°íëì§ ìì ë³ììì ìí¸ìì©
typeofë ì ê³µë í¼ì°ì°ìì ëí´ íì 문ìì´ì ë°ííë ê²ì´ ë³´ì¥ë©ëë¤. ì ì¸ëì§ ìì ìë³ìë¼ë typeofë ì¤ë¥ë¥¼ ëì§ì§ ìê³ "undefined"를 ë°íí©ëë¤.
typeof undeclaredVariable; // "undefined"
ê·¸ë¬ë ëì¼í ë¸ë¡ ë´ìì ì ì¸ ìì¹ ì´ì ì ë ì컬 ì ì¸(let const, class
)ì typeof를 ì¬ì©íë©´ ReferenceError를 ëì§ëë¤. ë¸ë¡ ë²ì ë³ìë ì´ê¸°íê° ì²ë¦¬ë기 ì ê¹ì§ ë¸ë¡ ììë¶í° ì¼ìì ì¬ê°ì§ë(TDZ) ì ì기 ë문ì, ì´ ê¸°ê°ì ì ê·¼íë©´ ì¤ë¥ê° ë°ìí©ëë¤.
typeof newLetVariable; // ReferenceError
typeof newConstVariable; // ReferenceError
typeof newClass; // ReferenceError
let newLetVariable;
const newConstVariable = "hello";
class newClass {}
document.allì ìì¸ì ì¸ ëì
íì¬ ëª¨ë ë¸ë¼ì°ì ë íì
ì´ undefinedì¸ ë¹íì¤ í¸ì¤í¸ ê°ì²´ document.all
ì ë
¸ì¶í©ëë¤.
typeof document.all === "undefined";
document.allì falsy ê°ì´ê³ undefined ì ëì¨í ëë±ì ê°ì§ì§ë§, ì¤ì ë¡ undefinedë ìëëë¤. document.allì íì
ì´ "undefined"ì¸ ê²½ì°ë ì¹ í¸íì±ì ìí ìë ECMAScript íì¤ì "ìëì ì¸ ìë°(willful violation)"ì¼ë¡ ì¹ íì¤ì ë¶ë¥ëì´ ììµëë¤.
ë 구체ì ì¸ íì ì ì»ë ì¬ì©ì ì ì ë©ìë
typeofë ë§¤ì° ì ì©íì§ë§, íìí ë§í¼ ë¤ì¬ë¤ë¥íì§ë ììµëë¤. ì를 ë¤ì´, typeof []ë "object"ì´ê³ , typeof new Date(), typeof /abc/ë ë§ì°¬ê°ì§ì
ëë¤.
ë 구체ì ì¼ë¡ íì
ì íì¸í기 ìí´ ì¬ê¸°ììë ì¬ì©ì ì ì type(value) í¨ì를 ì ìí©ëë¤. ì´ í¨ìë ëë¶ë¶ typeofì ëìì 모방íì§ë§, ë¹ììê°(ì¦, ê°ì²´ì í¨ì)ì ëí´ìë ê°ë¥í ê²½ì° ë ì¸ë¶íë íì
ì´ë¦ì ë°íí©ëë¤.
function type(value) {
if (value === null) {
return "null";
}
const baseType = typeof value;
// ìì íì
if (!["object", "function"].includes(baseType)) {
return baseType;
}
// Symbol.toStringTagë ì¢
ì¢
ê°ì²´ í´ëì¤ì "íì ì´ë¦"ì ì§ì í©ëë¤.
// Object.prototype.toString()ìì ì¬ì©ë©ëë¤.
const tag = value[Symbol.toStringTag];
if (typeof tag === "string") {
return tag;
}
// ìì¤ ì½ëê° "class" í¤ìëë¡ ììíë í¨ìë¼ë©´ í´ëì¤ì
ëë¤.
if (
baseType === "function" &&
Function.prototype.toString.call(value).startsWith("class")
) {
return "class";
}
// ìì±ìì ì´ë¦; ì: `Array`, `GeneratorFunction`,
// `Number`, `String`, `Boolean`, ëë `MyCustomClass`
const className = value.constructor.name;
if (typeof className === "string" && className !== "") {
return className;
}
// ì´ ìì ììë ê°ì ì»ì íì¤í ë°©ë²ì´ ìì¼ë¯ë¡,
// 기본 구íì ì¬ì©í©ëë¤.
return baseType;
}
ì¡´ì¬íì§ ìì ì ìë ë³ì를 íì¸í ëë ReferenceErrorê° ë°ìíì§ ìëë¡ typeof nonExistentVar === "undefined"를 ì¬ì©í´ì¼ í©ëë¤. ì´ ëìì ì¬ì©ì ì ì ì½ëë¡ë íë´ë¼ ì ììµëë¤.
ëª ì¸ì
| Specification |
|---|
| ECMAScript® 2026 Language Specification > # sec-typeof-operator > |