How to check if a variable is a function in JavaScript

Checking if a variable is a function is essential for JavaScript applications that use callbacks, event handlers, or dynamic function execution patterns. As the creator of CoreUI, a widely used open-source UI library, I’ve implemented function type checking in countless JavaScript utilities, event systems, and component APIs. From my 25 years of experience in web development, the most straightforward and reliable approach is to use the typeof operator. This method works consistently across all JavaScript environments and function types.

Use the typeof operator to check if a variable is a function.

function isFunction(value) {
  return typeof value === 'function'
}

// Usage examples
console.log(isFunction(function() {}))        // true
console.log(isFunction(() => {}))             // true
console.log(isFunction(console.log))          // true
console.log(isFunction(Math.max))             // true
console.log(isFunction('string'))             // false
console.log(isFunction({}))                   // false
console.log(isFunction(null))                 // false

// Practical usage
function executeCallback(callback) {
  if (isFunction(callback)) {
    callback()
  } else {
    console.error('Provided callback is not a function')
  }
}

The typeof operator returns the string “function” for all function types including function declarations, function expressions, arrow functions, and built-in functions. This check works reliably across different JavaScript environments and doesn’t require any special handling for different function syntaxes. The function returns a boolean value that can be used in conditional statements for safe function execution or validation.

This is the same reliable function checking approach we use in CoreUI components for validating callback props and event handlers. For more specific checks, you can also use Object.prototype.toString.call(value) === '[object Function]', but typeof is simpler and sufficient for most use cases.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.

Answers by CoreUI Core Team