Загрузка…
Загрузка…
typeof возвращает строку с типом операнда. Удобен для быстрых проверок примитивов.
typeof 1; // "number"
typeof "hi"; // "string"
typeof true; // "boolean"
typeof undefined; // "undefined"
typeof Symbol(); // "symbol"
typeof 10n; // "bigint"
typeof function(){}; // "function"
typeof {}; // "object"
typeof null; // "object" ← исторический баг
typeof []; // "object"typeof null === "object" — не путать с реальным объектом; проверяйте value === null."object" → Array.isArray(x)."function" (хотя это callable objects).typeof notDeclared → "undefined" без ReferenceError (особый безопасный кейс).function isObject(v) {
return v !== null && typeof v === "object" && !Array.isArray(v);
}Итог: typeof — строковая метка типа; помните про null, массивы и отличие от instanceof.
