[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Operators/typeof [Back]  [Original]

typeof - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

typeof

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 7.

typeof .

In this article

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"

js
typeof operand

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"

. .

js
// 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

js
//  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 .

js
const str = new String("String");
const num = new Number(100);

typeof str; // "object"
typeof num; // "object"

const func = new Function();

typeof func; // "function"

typeof (+) . .

js
//         .
const someData = 99;

typeof someData + " foo"; // "number foo"
typeof (someData + " foo"); // "string"

typeof . typeof "undefined" .

js
typeof undeclaredVariable; // "undefined"

(let const, class ) typeof ReferenceError . (TDZ) , .

js
typeof newLetVariable; // ReferenceError
typeof newConstVariable; // ReferenceError
typeof newClass; // ReferenceError

let newLetVariable;
const newConstVariable = "hello";
class newClass {}

document.all

undefined document.all .

js
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 , (, ) .

js
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 2027 LanguageSpecification
# sec-typeof-operator


Web Proxy Viewer  |  New URL  |  Original Page