Implement Type Utilities
Six one-line functions that answer 'what type is this?' at runtime. The whole exercise is knowing where `typeof` tells the truth and where it lies — it's honest about numbers and symbols, but `typeof null` famously returns `'object'`, so `isNull` needs a second check to actually mean what it says.
The problem
Implement `isBoolean`, `isNumber`, `isNull`, `isString`, `isSymbol`, and `isUndefined`, each returning whether a given `value` is that primitive type. `isNumber` should treat `NaN` as a number — it's `typeof NaN === 'number'` even though `NaN !== NaN`, so the naive `typeof` check already does the right thing without any extra work.
The trap is `null`. JavaScript's `typeof null` returns `'object'` — a bug baked into the language since 1995 — so a function that just did `typeof value === 'object'` would call every plain object `null` too. `isNull` has to narrow that down with an explicit `value === null` check.
The approach
Five of the six are a direct `typeof value === '<type>'` — booleans, numbers (NaN included, since it's still typeof 'number'), strings, symbols, and undefined all report themselves correctly and unambiguously through `typeof`.
`isNull` is the one exception: `typeof value === 'object'` is true for both `null` and every object or array, so the function narrows with `value === null` to isolate just the one case `typeof` can't distinguish on its own.
The solution
/**
* @param {unknown} value
* @returns {boolean}
*/
export function isBoolean(value) {
return typeof value === "boolean";
}
/**
* @param {unknown} value
* @returns {boolean}
*/
export function isNumber(value) {
return typeof value === "number";
}
/**
* @param {unknown} value
* @returns {boolean}
*/
export function isNull(value) {
return typeof value === "object" && value === null;
}
/**
* @param {unknown} value
* @returns {boolean}
*/
export function isString(value) {
return typeof value === "string";
}
/**
* @param {unknown} value
* @returns {boolean}
*/
export function isSymbol(value) {
return typeof value === "symbol";
}
/**
* @param {unknown} value
* @returns {boolean}
*/
export function isUndefined(value) {
return typeof value === "undefined";
}Time O(1)Space O(1)