Last updated: Feb 28, 2024
Reading timeยท3 min
To check if an object is empty in TypeScript:
Object.keys()
method to get an array of the object's keys.length
property on the array.length
property is equal to 0
, the object is empty.interface Person { name?: string; age?: number; country?: string; } const person: Person = {}; if (Object.keys(person).length === 0) { console.log('Object is empty'); // ๐๏ธ this runs } else { console.log('Object is NOT empty'); } const isEmpty = Object.keys(person).length === 0; console.log(isEmpty); // ๐๏ธ true
We used the Object.keys() method to get an array of the object's keys.
// ๐๏ธ ['name', 'age'] console.log(Object.keys({ name: 'Bobby Hadz', age: 30 }));
Object.keys
method returns an empty array if the object has no key-value pairs (if it's empty).If accessing the length
property on the array of keys returns anything other
than 0
, then the object is not empty.
You can create a reusable function if have to perform the check often.
interface Person { name?: string; age?: number; country?: string; } const person: Person = {}; function isEmpty(obj: Record<string, any>): boolean { return Object.keys(obj).length === 0; } // ๐๏ธ true console.log(isEmpty(person)); // ๐๏ธ false console.log(isEmpty({ name: 'bobby hadz' }));
The function takes an object and returns true
if the object is empty and
false
otherwise.
Some objects that are created using the constructor functions that would return a false positive if we only check for the length of the object's keys.
function isEmpty(obj: Record<string, any>): boolean { return Object.keys(obj).length === 0; } console.log(isEmpty(new String())); // ๐๏ธ true console.log(isEmpty(new Number())); // ๐๏ธ true
Chances are you don't use these constructor functions with the new
keyword in
your code and that isn't an issue you have to worry about.
However, you could handle the edge case by checking if the object's constructor
is equal to Object
.
function isEmpty(obj: Record<string, any>): boolean { return Object.keys(obj).length === 0 && obj.constructor === Object; } console.log(isEmpty({})); // ๐๏ธ true console.log(isEmpty({ name: 'bobby hadz' })); // ๐๏ธ false console.log(isEmpty(new String())); // ๐๏ธ false
We used the logical AND (&&) operator, so the isEmpty
function only returns
true
if both conditions are met.
Here's what calling Object.keys()
on a non-empty object looks like.
const obj = {a: 1, b: 2}; console.log(Object.keys(obj)); // ๐๏ธ ['a', 'b'] const isEmpty = Object.keys(obj).length === 0; console.log(isEmpty); // ๐๏ธ false
I've also written an article on how to check if a property exists in an object.
An alternative approach is to try to iterate over the properties of the object. If there is even a single iteration, then the object is not empty.
for...in
loop in TypeScriptThis is a three-step process:
for...in
loop to iterate over the properties of the object.interface Person { name?: string; age?: number; country?: string; } const person: Person = {}; function isEmpty(obj: Record<string, any>): boolean { for (const _key in obj) { return false; } return true; } console.log(isEmpty(person)); // ๐๏ธ true console.log(isEmpty({ name: 'James' })); // ๐๏ธ false
The isEmpty
function takes an object and attempts to iterate over the
properties of the passed-in object.
If there is even 1
iteration, we know that the object has at least 1
key-value pair and is not an
empty object.
If there aren't any iterations, then the object is empty.
I'd stick to the Object.keys()
approach because it is more direct and
intuitive.
lodash
in TypeScriptAlternatively, you can use the lodash.isEmpty()
method.
Open your terminal in the root directory of your project and install lodash
with the following 2 commands.
npm install lodash.isempty npm install --save-dev @types/lodash.isempty
Now you can import and use the isEmpty()
method from lodash
.
import isEmpty from 'lodash.isempty'; console.log(isEmpty({})); // ๐๏ธ true console.log(isEmpty({ name: 'bobby hadz' })); // ๐๏ธ false
The lodash.isempty
method checks if the supplied value is an empty object,
collection, Map
or Set
.
You can learn more about the related topics by checking out the following tutorials: