[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/typeof#typeof_null [Back]  [Original]

typeof - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

typeof

20157

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"

js
typeof operand

operand

typeof JavaScript

Undefined "undefined"
Null "object"
Boolean "boolean"
Number "number"
BigInt "bigint"
String "string"
Symbol "symbol"
Function ECMA-262 [[Call]]classes) "function"
"object"

typeof Internet Explorer

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"; //  ! Boolean()

// Symbols
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 0x00null 0typeof null "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 + " Wisen"; // "number Wisen"
typeof (someData + " Wisen"); // "string"

typeof typeof "undefined"

js
typeof undeclaredVariable; // "undefined"

let const let const typeof ReferenceError

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 undefined undefined Web document.all "undefined" ECMAScript Web

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 display name
  //  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