Javascript – Check if a value is an object in JavaScript

javascriptjavascript-objectstypes

How do you check if a value is an object in JavaScript?

Best Answer

If typeof yourVariable === 'object', it's an object or null.

If you want null, arrays or functions to be excluded, just make it:

if (
    typeof yourVariable === 'object' &&
    !Array.isArray(yourVariable) &&
    yourVariable !== null
) {
    executeSomeCode();
}