English

Code Pool - Utilities

Javascript plain object!

A cover featuring code examples of plain JavaScript objects with the text: "JS lies about objects."
© Code pool series - utility to find plain-objects in JavaScript and TypeScript.
0
6th Jul 2025

You can read about code-pool concepts on this link.

 

About this post

In this post, we’ll create a utility to ensure a given value is an actual object—not null or an array. This will prevent unexpected behavior.

 

Table of Contents

What is a plain object in JavaScript?

JS is somewhat weird in its nature. As you may know, not only objects but also null and arrays have the type object. If you don’t believe me, try running the following code:

console.log(typeof null);
console.log(typeof []);

This behavior can lead to unpredictable issues. For example, when you create a function to merge two objects, if you don’t ensure the inputs are actually objects, you may encounter errors or unexpected results. The following function helps you verify that your input is a real object.

Utility: isPlainObject()

Here it is, simple and straightforward.

export const isPlainObject = (value: unknown): value is Record<string, unknown> =>
typeof value === 'object' && value !== null && !Array.isArray(value);

 

You can find the code and useful JSDoc at this GitHub link.

Rating 5 of 5
#development#for_web#code_pool
Javascript plain object! | The Latest Articles About Development And Architecture | Hosein Pouyanmehr