কম্পিউটার টিউটোরিয়াল

জাভাস্ক্রিপ্টে ভেরিয়েবল ফাংশন টাইপ কিনা কীভাবে চেক করবেন?


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
  1. ক্রোমে জাভাস্ক্রিপ্ট ফাংশন ডেফিনিশন কিভাবে খুঁজে বের করবেন?

  2. জাভাস্ক্রিপ্টে ভ্যারিয়েবলটি অ্যারে কিনা যাচাই করার পদ্ধতি

  3. জাভাস্ক্রিপ্টে অ্যারে সত্যি অ্যারে কিনা যাচাই করার পদ্ধতি

  4. জাভাস্ক্রিপ্ট ভ্যারিয়েবল লোড হয়েছে কিনা যাচাই করা