typeof Operator
The typeof operator returns a string describing the type of a value at runtime.
It is safe to use on undeclared identifiers (returns "undefined" instead of throwing).
Syntax
typeof expression
typeof (expression) // parentheses optional
Examples
// ১) প্রিমিটিভ ও বিশেষ কেস
কনসোল.লগ(ধরন 42); // "number"
কনসোল.লগ(ধরন NaN); // "number"
কনসোল.লগ(ধরন "Hi"); // "string"
কনসোল.লগ(ধরন true); // "boolean"
কনসোল.লগ(ধরন undefined); // "undefined"
কনসোল.লগ(ধরন 10n); // "bigint"
কনসোল.লগ(ধরন Symbol("s")); // "symbol"
// ২) ফাংশন/অ্যারে/অবজেক্ট/নাল
ফাংশন f() {}
কনসোল.লগ(ধরন f); // "function"
কনসোল.লগ(ধরন []); // "object" ← অ্যারে হলেও object
কনসোল.লগ(ধরন {}); // "object"
কনসোল.লগ(ধরন null); // "object" ← ঐতিহাসিক বিশেষ কেস
// ৩) ঘোষণা নেই — তবু নিরাপদ
কনসোল.লগ(ধরন অজানা_চলক); // "undefined" (ReferenceError নয়)
কনসোল.লগ(ধরন window !== "undefined" ? window : undefined);
// ৪) typeof‑গার্ড প্যাটার্ন
function লগ(x) {
যদি (ধরন x === "string" || ধরন x === "number") {
দেখাও("প্রিন্ট:", x);
} নাহলে যদি (ধরন x === "function") {
লিখো("ফাংশন:", x.name);
} নাহলে {
কনসোল.সতর্ক("টাইপ সাপোর্ট নেই:", ধরন x);
}
}
// ৫) অ্যারে/তারিখ/রেজেক্স আলাদা করতে
কনসোল.লগ(Array.isArray([])); // true
কনসোল.লগ((new Date()) instanceof Date); // true
কনসোল.লগ((/re/) instanceof RegExp); // true
Notes
typeofalways returns one of:"undefined","boolean","number","bigint","string","symbol","function","object".- Historical quirk:
typeof null === "object". Arrays, dates, and regexes also report"object"— useArray.isArrayorinstanceofto disambiguate. - Safe for undeclared variables:
typeof notDeclared === "undefined". Directly readingnotDeclaredwould throwReferenceError. - Classes are functions:
typeof class X {} === "function". Instances:"object". NaNis a number; check withNumber.isNaN.- Keep one language style (English/Bangla) per file for clarity.