959 B
959 B
title, tags, cover, firstSeen, lastUpdated
| title | tags | cover | firstSeen | lastUpdated |
|---|---|---|---|---|
| Check if value is of type | type | coffee-phone-tray | 2018-01-17T21:23:46+02:00 | 2020-10-20T23:02:01+03:00 |
Checks if the provided value is of the specified type.
- Ensure the value is not
undefinedornullusingArray.prototype.includes(). - Use
Object.prototype.constructorto compare the constructor property on the value withtypeto check if the provided value is of the specifiedtype.
const is = (type, val) => ![, null].includes(val) && val.constructor === type;
is(Array, [1]); // true
is(ArrayBuffer, new ArrayBuffer()); // true
is(Map, new Map()); // true
is(RegExp, /./g); // true
is(Set, new Set()); // true
is(WeakMap, new WeakMap()); // true
is(WeakSet, new WeakSet()); // true
is(String, ''); // true
is(String, new String('')); // true
is(Number, 1); // true
is(Number, new Number(1)); // true
is(Boolean, true); // true
is(Boolean, new Boolean(true)); // true