JS: Test is Object Type 📜

By Xah Lee. Date: . Last updated: .
/* xah_is_obj(x) return true if x is object type.
Version: 2023-01-17
 */
const xah_is_obj = (x) => {
 const xtype = typeof x;
 return (x !== null) && (xtype === "object" || xtype === "function");
};

// s------------------------------
// test

[
 {},
 [],
 /./,
 new Date(),
 function f() {},
 (x) => 3,
 new Set(),
 new Map(),
 new WeakMap(),
 new WeakSet(),
 function* () {},
 (function* () {})(),
 JSON,
 Math,
 Error(),
 new Promise(() => {}),
].forEach((x) => {
 console.assert(xah_is_obj(x));
});

[
 null,
 3,
 1,
 Infinity,
 NaN,
 "",
 true,
 undefined,
 Symbol(),
].forEach((x) => console.assert(xah_is_obj(x) === false));