Skip to content

equalsShallow

One-level (shallow) object equality.

Two objects are equal when they share the exact same set of own enumerable string keys and each pair of values satisfies strict equality (===). No recursion: nested objects/arrays are compared by reference.

Falls back to strict equality when either input is null, undefined or not an object \u2014 so primitives match if and only if they are ===. Arrays are not supported; they always return false (unless identical references). Use array/equalsShallow instead.

For recursive structural comparison use equalsDeep. For a diff structure use diff.

Available since v2.0.0

import { equalsShallow } from '@helpers4/object';
// or, from the all-in-one package (same code, one install):
import { equalsShallow } from 'helpers4/object';
equalsShallow(objA: unknown, objB: unknown): boolean
ParameterTypeDescription
objAunknownFirst value to compare
objBunknownSecond value to compare

boolean — true if values are shallowly equal, false otherwise.

Uses JSON.stringify for a fast comparison.

equalsShallow({ a: 1, b: 2 }, { a: 1, b: 2 })
// => true

View source on GitHub