Walle Cyril Walle Cyril - 1 year ago 64
Javascript Question

How to check if x is an Object but not a String Object

What I have so far.

const isNotNullObject = function (x) {
return (typeof x === "object" && x !== null);

It works fine for arrays and objects. But for String objects too !

isNotNullObject(new String(5))

What I want is false for any type of string. Note that I don't have control of the calling code. I can't remove
myself. I need a solution that does not create a new String just to check for equality if possible for performance reasons.

Answer Source

Use instance of

return (typeof x === "object" && !(x instanceof String) && x !== null)

const isNotNullObject = function(x) {
  return (typeof x === "object" && !(x instanceof String) && x !== null);

  isNotNullObject(new String(5))

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download