Avatar ·

How to check if a variable is an object in JavaScript?

Hello everyone! I'm a beginner in JavaScript and encountered a problem when checking data types. I need to write a function that takes an argument and checks whether it is an object (not an array, not null, not a primitive). \n\nI tried using `typeof`, but it returns `"object"` for arrays and `null`, which doesn't work for me. Here's an example:\n\n```javascript\nfunction isObject(value) {\n return typeof value === 'object';\n}\n\nconsole.log(isObject({})); // true — correct\nconsole.log(isObject([])); // true — but should be false, it's an array\nconsole.log(isObject(null)); // true — but should be false, null is not an object\nconsole.log(isObject(function(){})); // false — but it's a function, it shouldn't be considered an object either\n```\n\nI tried adding a check with `Array.isArray()`, but that doesn't solve the problem.