| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Operators/typeof | [Back] [Original] |
Get to know MDN better
console.log(typeof 42);
// : "number"
console.log(typeof "blubber");
// : "string"
console.log(typeof true);
// : "boolean"
console.log(typeof undeclaredVariable);
// : "undefined"
typeof operand
typeof JavaScript
| Undefined | "undefined" |
| Null | "object" () |
"boolean" |
|
"number" |
|
"bigint" |
|
"string" |
|
"symbol" |
|
| (ECMA-262 [[Call]] ) | "function" |
"object" |
//
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";
// JavaScript
typeof null === "object";
JavaScript JavaScript 0 null NULL ( 0x00) null 0 typeof "object" ()
ECMAScript () typeof null === "null"
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"
//
const someData = 99;
typeof someData + " foo"; // "number foo"
typeof (someData + " foo"); // "string"
typeof "undefined"
typeof undeclaredVariable; // "undefined"
typeof letconstusingawait usingclassReferenceError
typeof newLetVariable; // ReferenceError
typeof newConstVariable; // ReferenceError
typeof newClass; // ReferenceError
let newLetVariable;
const newConstVariable = "hello";
class newClass {}
document.all undefined
typeof document.all === "undefined";
document.all undefined undefined document.all "undefined" ECMAScript
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"
| ECMAScript 2027 LanguageSpecification # sec-typeof-operator |
| Web Proxy Viewer | New URL | Original Page |