diff --git a/snippets/isPrimitive.md b/snippets/isPrimitive.md new file mode 100644 index 000000000..3abe155c4 --- /dev/null +++ b/snippets/isPrimitive.md @@ -0,0 +1,22 @@ +### isPrimitive + +Returns a boolean determining if the supplied value is primitive or not. + +Use `Array.includes()` on an array of type strings which are not primitive, +supplying the type using `typeof`. +Since `typeof null` evaluates to `'object'`, it needs to be directly compared. + +```js +const isPrimitive = val => !['object', 'function'].includes(typeof val) || val === null; +``` + +```js +isPrimitive(window.someNonExistentProperty); // true +isPrimitive(null); // true +isPrimitive(50); // true +isPrimitive('Hello!'); // true +isPrimitive(false); // true +isPrimitive(Symbol()); // true +isPrimitive([]); // false +isPrimitive(new String('Hello!')); // false +``` diff --git a/tag_database b/tag_database index 7e280efbd..1e9a4367c 100644 --- a/tag_database +++ b/tag_database @@ -75,6 +75,7 @@ isFunction:utility isNull:utility isNumber:utility isPrime:math +isPrimitive:utility isPromiseLike:utility isString:utility isSymbol:utility