Determines whether a JavaScript value is callable (can be invoked as a function)
npx @tessl/cli install tessl/npm-is-callable@1.2.0is-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.
npm install is-callablevar 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');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)); // trueThe is-callable package exports a single function as its default export. This is the complete API surface.
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:
function() {}() => {}function*() {}async function() {}, async () => {}Array, Object, String, etc.Int8Array, Uint8Array, etc..bind())document.all (in certain browser environments)Returns false for:
class Foo {}null, undefined, 42, "string", true, false{}, [], /regex/, new Date()@@toStringTag set to "Function" (fake functions)Key Features:
Browser-Specific Behavior:
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]);
}