typeof অপারেটর ব্যবহার
typeof ব্যবহার করে চেক করুন যে এটি ফাংশন কিনা।
উদাহরণ
<html>
<body>
<script>
var res = function() {
// ফাংশন বডি
};
if (typeof res === "function") {
alert("A function");
}
</script>
</body>
</html>
typeof ফলাফল
| মান | typeof ফলাফল |
|---|---|
| function() {} | "function" |
| function name() {} | "function" |
| () => {} | "function" |
| class MyClass {} | "function" |
| null | "object" |
| undefined | "undefined" |
| {} | "object" |
| [] | "object" |
আরও উদাহরণ
function namedFunc() {}
var anonFunc = function() {};
const arrowFunc = () => {};
console.log(typeof namedFunc); // "function"
console.log(typeof anonFunc); // "function"
console.log(typeof arrowFunc); // "function"
console.log(typeof null); // "object" (জানানো বাগ)
console.log(typeof undefined); // "undefined"
ফাংশন চেক করার সেরা পদ্ধতি
// instanceof (শুধু ফাংশন অবজেক্টের জন্য) func instanceof Function // true // কনস্ট্রাক্টর চেক func.constructor === Function // true