Files
30-seconds-of-code/snippets/is-primitive.md
Angelos Chalaris 61200d90c4 Kebab file names
2023-04-27 21:58:35 +03:00

27 lines
631 B
Markdown

---
title: Number is primitive
tags: type
cover: flower-camera
firstSeen: 2017-12-31T12:48:13+02:00
lastUpdated: 2020-10-22T20:23:47+03:00
---
Checks if the passed value is primitive or not.
- Create an object from `val` and compare it with `val` to determine if the passed value is primitive (i.e. not equal to the created object).
```js
const isPrimitive = val => Object(val) !== val;
```
```js
isPrimitive(null); // true
isPrimitive(undefined); // true
isPrimitive(50); // true
isPrimitive('Hello!'); // true
isPrimitive(false); // true
isPrimitive(Symbol()); // true
isPrimitive([]); // false
isPrimitive({}); // false
```