diff --git a/snippets/isPrimitive.md b/snippets/isPrimitive.md new file mode 100644 index 000000000..cd90501b3 --- /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, supplying the type using `typeof`. +Since `typeof null` evaluates to `'object'`, it needs to be directly compared. + +```js +const isPrimitive = value => + ['string', 'number', 'symbol', 'boolean', 'undefined'].includes(typeof value) || value === 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 d897e319c..9287acd90 100644 --- a/tag_database +++ b/tag_database @@ -71,6 +71,7 @@ isEven:math isFunction:utility isNumber:utility isPrime:math +isPrimitive:utility isString:utility isSymbol:utility JSONToDate:date