Type Utilities
JS Functions
Easy
15 min
types
language fundamentals
Implement a small set of type predicates for JavaScript's primitive types. Export each one by name, and export isPrimitive as the default.
import isPrimitive, {
isBoolean, isNumber, isString, isSymbol, isBigInt, isNull, isUndefined, isNil,
} from './index';
isNumber(42); // true
isNumber(NaN); // true -> NaN is of type number
isNumber('42'); // false
isNull(null); // true
isNil(undefined); // true
isBoolean(new Boolean(false)); // false -> boxed objects are not primitives
isPrimitive({}); // false
isPrimitive(10n); // true
Requirements
Each function takes one value and returns a boolean.
| Function | Returns true for |
|---|---|
isBoolean | true and false |
isNumber | any primitive number, including NaN and ±Infinity |
isString | primitive strings |
isSymbol | symbols |
isBigInt | bigints |
isNull | null only |
isUndefined | undefined only |
isNil | null or undefined |
isPrimitive (default export) | any of the seven primitive types: string, number, bigint, boolean, symbol, undefined, null |
- Boxed primitives (
new Number(1),new String('a'),Object(Symbol())) are objects, so every predicate above returnsfalsefor them. document.alland other host oddities are out of scope.
Follow-up
- Why does
typeof nullreturn'object'? - How would you check for a finite number, or an integer?
- Why can't
x === undefinedalways be trusted in very old code, and why istypeof x === 'undefined'still used for undeclared globals?
Spotted a mistake or a rights concern? Report an issue