or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

index.md
tile.json

index.mddocs/

0

# is-callable

1

2

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.

3

4

## Package Information

5

6

- **Package Name**: is-callable

7

- **Package Type**: npm

8

- **Language**: JavaScript

9

- **Installation**: `npm install is-callable`

10

11

## Core Imports

12

13

```javascript

14

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

15

```

16

17

ES6 modules (with bundlers or transpilers):

18

19

```javascript

20

import isCallable from 'is-callable';

21

```

22

23

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

24

25

```javascript

26

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

27

```

28

29

## Basic Usage

30

31

```javascript

32

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

33

34

// Functions are callable

35

console.log(isCallable(function() {})); // true

36

console.log(isCallable(() => {})); // true

37

console.log(isCallable(function*() {})); // true

38

console.log(isCallable(async function() {})); // true

39

40

// Non-functions are not callable

41

console.log(isCallable(null)); // false

42

console.log(isCallable(undefined)); // false

43

console.log(isCallable(42)); // false

44

console.log(isCallable("string")); // false

45

console.log(isCallable({})); // false

46

console.log(isCallable([])); // false

47

48

// ES6 classes are not callable (when detectable)

49

console.log(isCallable(class Foo {})); // false

50

51

// Built-in constructors are callable

52

console.log(isCallable(Array)); // true

53

console.log(isCallable(Object)); // true

54

console.log(isCallable(String)); // true

55

```

56

57

## Capabilities

58

59

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

60

61

### Callable Detection

62

63

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

64

65

```javascript { .api }

66

/**

67

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

68

* @param {any} value - The value to test for callability

69

* @returns {boolean} true if the value is callable, false otherwise

70

*/

71

function isCallable(value);

72

```

73

74

**Returns `true` for:**

75

- Regular functions: `function() {}`

76

- Arrow functions: `() => {}`

77

- Generator functions: `function*() {}`

78

- Async functions: `async function() {}`, `async () => {}`

79

- Built-in constructors: `Array`, `Object`, `String`, etc.

80

- Typed array constructors: `Int8Array`, `Uint8Array`, etc.

81

- Method functions from prototypes

82

- Bound functions (created with `.bind()`)

83

- Proxies of callable objects

84

- `document.all` (in certain browser environments)

85

86

**Returns `false` for:**

87

- ES6 class constructors (when detectable): `class Foo {}`

88

- Primitive values: `null`, `undefined`, `42`, `"string"`, `true`, `false`

89

- Objects: `{}`, `[]`, `/regex/`, `new Date()`

90

- Objects with `@@toStringTag` set to "Function" (fake functions)

91

- Non-function objects with functions in their prototype chain

92

93

**Key Features:**

94

- **Cross-browser compatibility**: Works across IE 6+, Chrome, Firefox, Safari, Opera

95

- **ES6 class detection**: Correctly identifies ES6 classes as non-callable when detectable

96

- **Symbol.toStringTag handling**: Properly handles objects with Symbol.toStringTag manipulation

97

- **Performance optimized**: Uses Reflect.apply when available for better performance

98

- **Edge case handling**: Handles document.all, HTML element constructors, and other DOM quirks

99

- **Safe**: Never throws exceptions, returns false for any invalid input

100

101

**Browser-Specific Behavior:**

102

- **Safari 9**: Classes may be incorrectly reported as callable due to Function.prototype.toString behavior

103

- **Firefox 45-54**: Classes may be incorrectly reported as callable

104

- **Firefox 42-63**: Function.prototype.toString throws on HTML element constructors or Proxies to functions

105

- **Firefox 20-35**: HTML element constructors are not callable despite having typeof "function"

106

- **Firefox 19**: document.all is not callable

107

- **IE 6-8**: Special handling for document.all and HTML collections with typeof "object"

108

109

**Usage Examples:**

110

111

```javascript

112

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

113

114

// Type checking in a utility function

115

function safeInvoke(fn, args) {

116

if (isCallable(fn)) {

117

return fn.apply(null, args);

118

}

119

throw new TypeError('Expected a callable function');

120

}

121

122

// Filtering an array for functions

123

var mixed = [42, function() {}, "string", () => {}, null, class Foo {}];

124

var functions = mixed.filter(isCallable);

125

// Result: [function() {}, () => {}]

126

127

// Validating callback parameters

128

function processData(data, callback) {

129

if (!isCallable(callback)) {

130

throw new TypeError('Callback must be a function');

131

}

132

// Process data and call callback

133

callback(processedData);

134

}

135

136

// Checking if a method exists and is callable

137

function hasMethod(obj, methodName) {

138

return obj != null && isCallable(obj[methodName]);

139

}

140

```