Files
30-seconds-of-code/javascript/snippets/is-object.md
2023-05-01 22:35:56 +03:00

27 lines
640 B
Markdown

---
title: Value is object
type: snippet
tags: [type,object]
cover: flower-portrait-9
dateModified: 2021-01-08T00:23:44+02:00
---
Checks if the passed value is an object or not.
- Uses the `Object` constructor to create an object wrapper for the given value.
- If the value is `null` or `undefined`, create and return an empty object.
- Otherwise, return an object of a type that corresponds to the given value.
```js
const isObject = obj => obj === Object(obj);
```
```js
isObject([1, 2, 3, 4]); // true
isObject([]); // true
isObject(['Hello!']); // true
isObject({ a: 1 }); // true
isObject({}); // true
isObject(true); // false
```