What is is-callable?
The is-callable npm package is used to determine if a value is callable, meaning it can be called as a function. This is useful for type-checking in JavaScript where the typeof operator can be insufficient for certain cases, such as distinguishing between callable and non-callable objects.
Check if a value is callable
This feature allows you to check if various types of values are callable, including regular functions, async functions, arrow functions, and classes. It also correctly identifies non-callable objects like regex literals and plain objects.
const isCallable = require('is-callable');
console.log(isCallable(function() {})); // true
console.log(isCallable(async function() {})); // true
console.log(isCallable(() => {})); // true
console.log(isCallable(class MyClass {})); // true
console.log(isCallable(/regex/)); // false
console.log(isCallable(null)); // false
console.log(isCallable({})); // false