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

typeof - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

typeof

Baseline

20157

typeof

console.log(typeof 42);
// : "number"

console.log(typeof "blubber");
// : "string"

console.log(typeof true);
// : "boolean"

console.log(typeof undeclaredVariable);
// : "undefined"

js
typeof operand

operand

typeof JavaScript

Undefined "undefined"
Null "object" ()
"boolean"
"number"
"bigint"
"string"
"symbol"
(ECMA-262 [[Call]] ) "function"
"object"

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

// 
typeof "" === "string";
typeof "bla" === "string";
typeof `template literal` === "string";
typeof "1" === "string"; // 
typeof typeof 1 === "string"; // typeof 
typeof String(1) === "string"; // String toString 

// 
typeof true === "boolean";
typeof false === "boolean";
typeof Boolean(1) === "boolean"; // Boolean 
typeof !!1 === "boolean"; // ! ( NOT)  2  Boolean() 

// 
typeof Symbol() === "symbol";
typeof Symbol("foo") === "symbol";
typeof Symbol.iterator === "symbol";

// Undefined
typeof undefined === "undefined";
typeof declaredButUndefinedVariable === "undefined";
typeof undeclaredVariable === "undefined";

// 
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";

// 
typeof function () {} === "function";
typeof class C {} === "function";
typeof Math.sin === "function";

typeof null

js
// JavaScript 
typeof null === "object";

JavaScript JavaScript 0 null NULL ( 0x00) null 0 typeof "object" ()

ECMAScript () 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 "undefined"

js
typeof undeclaredVariable; // "undefined"

typeof letconstusingawait usingclassReferenceError

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

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

typeof undefined

document.all

document.all undefined

js
typeof document.all === "undefined";

document.all undefined undefined document.all "undefined" ECMAScript

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"

ECMAScript 2027 LanguageSpecification
# sec-typeof-operator


Web Proxy Viewer  |  New URL  |  Original Page