or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-is-callable

Determines whether a JavaScript value is callable (can be invoked as a function)

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/is-callable@1.2.x

To install, run

npx @tessl/cli install tessl/npm-is-callable@1.2.0

index.mddocs/

is-callable

is-callable is a reliable JavaScript utility that determines whether a value is callable (can be invoked as a function). It provides accurate detection across all JavaScript engines, handling edge cases like ES6 classes, generator functions, arrow functions, async functions, and browser-specific objects that typeof alone cannot handle properly.

Package Information

  • Package Name: is-callable
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install is-callable

Core Imports

var isCallable = require('is-callable');

ES6 modules (with bundlers or transpilers):

import isCallable from 'is-callable';

Or with destructuring (since it's the default export):

const { default: isCallable } = require('is-callable');

Basic Usage

var isCallable = require('is-callable');

// Functions are callable
console.log(isCallable(function() {})); // true
console.log(isCallable(() => {})); // true
console.log(isCallable(function*() {})); // true
console.log(isCallable(async function() {})); // true

// Non-functions are not callable
console.log(isCallable(null)); // false
console.log(isCallable(undefined)); // false
console.log(isCallable(42)); // false
console.log(isCallable("string")); // false
console.log(isCallable({})); // false
console.log(isCallable([])); // false

// ES6 classes are not callable (when detectable)
console.log(isCallable(class Foo {})); // false

// Built-in constructors are callable
console.log(isCallable(Array)); // true
console.log(isCallable(Object)); // true
console.log(isCallable(String)); // true

Capabilities

The is-callable package exports a single function as its default export. This is the complete API surface.

Callable Detection

Determines whether a JavaScript value is callable (can be invoked as a function).

/**
 * Determines whether a value is callable (can be invoked as a function)
 * @param {any} value - The value to test for callability
 * @returns {boolean} true if the value is callable, false otherwise
 */
function isCallable(value);

Returns true for:

  • Regular functions: function() {}
  • Arrow functions: () => {}
  • Generator functions: function*() {}
  • Async functions: async function() {}, async () => {}
  • Built-in constructors: Array, Object, String, etc.
  • Typed array constructors: Int8Array, Uint8Array, etc.
  • Method functions from prototypes
  • Bound functions (created with .bind())
  • Proxies of callable objects
  • document.all (in certain browser environments)

Returns false for:

  • ES6 class constructors (when detectable): class Foo {}
  • Primitive values: null, undefined, 42, "string", true, false
  • Objects: {}, [], /regex/, new Date()
  • Objects with @@toStringTag set to "Function" (fake functions)
  • Non-function objects with functions in their prototype chain

Key Features:

  • Cross-browser compatibility: Works across IE 6+, Chrome, Firefox, Safari, Opera
  • ES6 class detection: Correctly identifies ES6 classes as non-callable when detectable
  • Symbol.toStringTag handling: Properly handles objects with Symbol.toStringTag manipulation
  • Performance optimized: Uses Reflect.apply when available for better performance
  • Edge case handling: Handles document.all, HTML element constructors, and other DOM quirks
  • Safe: Never throws exceptions, returns false for any invalid input

Browser-Specific Behavior:

  • Safari 9: Classes may be incorrectly reported as callable due to Function.prototype.toString behavior
  • Firefox 45-54: Classes may be incorrectly reported as callable
  • Firefox 42-63: Function.prototype.toString throws on HTML element constructors or Proxies to functions
  • Firefox 20-35: HTML element constructors are not callable despite having typeof "function"
  • Firefox 19: document.all is not callable
  • IE 6-8: Special handling for document.all and HTML collections with typeof "object"

Usage Examples:

var isCallable = require('is-callable');

// Type checking in a utility function
function safeInvoke(fn, args) {
  if (isCallable(fn)) {
    return fn.apply(null, args);
  }
  throw new TypeError('Expected a callable function');
}

// Filtering an array for functions
var mixed = [42, function() {}, "string", () => {}, null, class Foo {}];
var functions = mixed.filter(isCallable);
// Result: [function() {}, () => {}]

// Validating callback parameters
function processData(data, callback) {
  if (!isCallable(callback)) {
    throw new TypeError('Callback must be a function');
  }
  // Process data and call callback
  callback(processedData);
}

// Checking if a method exists and is callable
function hasMethod(obj, methodName) {
  return obj != null && isCallable(obj[methodName]);
}