Skip to main content

Check if a value is an general object

Objects in javascript are 'object' type, but arrays, null, Dates, and other non-primitive types are also 'object' type. This function checks if a value is a general object.

/**
* Checks if a given value is an object.
*
* **Note**: `Date`s, `Array`s and other data structures are also considered objects.
*
* @param {unknown} val - The value to check.
* @returns {val is Record<string, unknown>} - Returns `true` if the value is an object, else `false`.
*
* @example
* isObject({}) // returns true
* isObject({ a: 1 }) // returns true
* isObject([]) // returns true
* isObject('Hello, world!') // returns false
*/
export const isObject = (val: unknown): val is Record<string, unknown> =>
!!val && typeof val === 'object'

Credits: Angelos Chalaris