Check that value is object literal?











up vote
23
down vote

favorite
7












I have a value and want to know if it's an iteratable object literal, before I iterate it.



How do I do that?










share|improve this question




















  • 2




    Please define "iterable".
    – Bergi
    Dec 5 '15 at 17:31















up vote
23
down vote

favorite
7












I have a value and want to know if it's an iteratable object literal, before I iterate it.



How do I do that?










share|improve this question




















  • 2




    Please define "iterable".
    – Bergi
    Dec 5 '15 at 17:31













up vote
23
down vote

favorite
7









up vote
23
down vote

favorite
7






7





I have a value and want to know if it's an iteratable object literal, before I iterate it.



How do I do that?










share|improve this question















I have a value and want to know if it's an iteratable object literal, before I iterate it.



How do I do that?







javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 1 '10 at 6:12









Ivo Wetzel

40.8k1184103




40.8k1184103










asked Dec 1 '10 at 2:48









ajsie

28.5k88244357




28.5k88244357








  • 2




    Please define "iterable".
    – Bergi
    Dec 5 '15 at 17:31














  • 2




    Please define "iterable".
    – Bergi
    Dec 5 '15 at 17:31








2




2




Please define "iterable".
– Bergi
Dec 5 '15 at 17:31




Please define "iterable".
– Bergi
Dec 5 '15 at 17:31












9 Answers
9






active

oldest

votes

















up vote
37
down vote













This should do it for you:



if( Object.prototype.toString.call( someObject ) === '[object Object]' ) {
// do your iteration
}


From ECMAScript 5 Section 8.6.2 if you're interested:




The value of the [[Class]] internal property is defined by this specification for every kind of built-in object. The value of the [[Class]] internal property of a host object may be any String value except one of "Arguments", "Array", "Boolean", "Date", "Error", "Function", "JSON", "Math", "Number", "Object", "RegExp", and "String". The value of a [[Class]] internal property is used internally to distinguish different kinds of objects. Note that this specification does not provide any means for a program to access that value except through Object.prototype.toString (see 15.2.4.2).







share|improve this answer

















  • 3




    This is no longer guaranteed to work in ES6, as an object can change the value of its Symbol.toStringTag property, which is used to generate Object.prototype.toString's return value. See the note at: people.mozilla.org/~jorendorff/… It looks like the way to check if something's iterable now is to see if it's Symbol.iterator property is defined
    – Ethan
    Mar 18 '15 at 9:48










  • Object.prototype.toString could be overridden; thus, not safe.
    – Frederik Krautwald
    May 12 '15 at 21:11






  • 5




    If its ever overwritten; that dev should be shot! Any prototypical structure can have its methods overwritten, and in turn would be useless if you could not depend on them.
    – Josh Mc
    Aug 5 '15 at 2:01






  • 1




    for the ppl who still using to this answer be careful : var str; toString.call( str) if the value is undefined or null it will comeback "object" in IE < 9 and some mobiles . checkout first for undefined and null before you apply this condition .
    – SUB-HDR
    Feb 26 at 21:30


















up vote
6
down vote













You could also do something like:



    if (someObject.constructor == Object) {
// do your thing
}


you can read more about it here






share|improve this answer

















  • 2




    If someObject is null, it doesn't have a constructor and therefore this throws a TypeError. You really want the first answer above.
    – chilts
    Jun 13 '13 at 3:19










  • This is far more elegant than the accepted answer for situations where you're trying to distinguish between arrays and objects in js.
    – ekillaby
    Jul 10 '13 at 18:44










  • @countfloortiles: yes and no - someObject.constructor = Object and the test will return true from there on.
    – CoDEmanX
    Dec 5 '14 at 23:39












  • @CoDEmanX, I don't understand what you mean.
    – ekillaby
    Dec 6 '14 at 0:23










  • @countfloortiles: .constructor can be overridden, so using it for type checking is not 100% safe.
    – CoDEmanX
    Dec 6 '14 at 14:27


















up vote
3
down vote













I think a function like this should be native, just like Array.isArray:



Object.isObject = function(obj) {
return obj && obj.constructor === this || false;
};


This one doesn't make function calls nor string comparisons, and doesn't reference global objects (like Object), so it should be quite fast. I don't think this will used in performance intensive tasks though, but whatever.



I'm not totally convinced about the name... maybe something like Object.isLiteral would be better.



It can cause problem with host objects, though, but I don't have a test ready.






share|improve this answer





















  • the || false part is pretty unnecessary.
    – user633183
    Jan 28 '14 at 16:21






  • 5




    @naomik It's not if you want the function to always return a boolean. If obj === null it'd return null instead. You can always go for return obj != null && object.constructor === this;.
    – MaxArt
    Jan 28 '14 at 21:04






  • 2




    The other way is return !!(obj && obj.constructor === this)
    – Aram Kocharyan
    Sep 9 '15 at 3:25


















up vote
2
down vote













Say you have some testvar and want to see if it is an object, but not an array or null (these are both of type Object). You can do the following



testVar instanceof Object && !Array.isArray(testVar) && testVar !== null





share|improve this answer




























    up vote
    0
    down vote













    Well, you don't need to check everything by yourself or write your own codes, when there are good libraries such as Lodash and Underscore.



    In Lodash, you can easily check it by isPlainObject function, e.g.:



    _.isPlainObject({'a': 12});


    Check this page: https://lodash.com/docs#isPlainObject






    share|improve this answer





















    • Just to be clear, Underscore has no such function - it does have _.isObject but that does not check for a plain object / object literal (e.g. it returns true also for an array)
      – JHH
      Apr 18 at 7:07


















    up vote
    0
    down vote













    Bumping old thread, but it's still shows up in searches and people are even referencing it as duplicate for a new similar ones - and still the top-most answers here are far from being correct (sorry people, no offends).



    To check if variable is an object the following should be used:



    if (typeof variable === 'object') {
    // do something
    }


    Arrays are also objects, so this would be true for an array too.
    Moreover - null is a valid object as well, therefore the above will return true on null too.
    Personally, when really need to check if an expected variable is 'workable' object I'm always using this boringly repeated formula:



    if (variable && typeof variable === `object`) {
    // do something
    }


    Last but not least :) please please please, do yourself a favor and don't use any libraries for such a simple things. Javascript is a fastly evolving language having today much much more than yesterday, so fast that most of the libraries are not even fast enough to fetch up.
    Beside it, people who are working on the spec are doing a great job and mostly the native APIs are clean, correct, making perfect sense and coherent with the rest of the language.






    share|improve this answer




























      up vote
      0
      down vote













      Little gist, not really elegant but efficient



      function _isObj( _obj ){

      return ( typeof _obj === "object" && JSON.stringify( _obj ).indexOf( "{" ) == 0 );

      }


      Little example






      function _isObj( _obj ){

      return ( typeof _obj === "object" && JSON.stringify( _obj ).indexOf( "{" ) == 0 );

      }

      var p = document.createElement( "p" );
      p.textContent = "undefined : " + _isObj( undefined );
      document.body.appendChild( p );

      p = document.createElement( "p" );
      p.textContent = "null : " + _isObj( null );
      document.body.appendChild( p );

      p = document.createElement( "p" );
      p.textContent = "boolean : " + _isObj( true );
      document.body.appendChild( p );

      p = document.createElement( "p" );
      p.textContent = "function : " + _isObj( function(){} );
      document.body.appendChild( p );

      p = document.createElement( "p" );
      p.textContent = "array : " + _isObj( );
      document.body.appendChild( p );

      p = document.createElement( "p" );
      p.textContent = "string : " + _isObj( "{}" );
      document.body.appendChild( p );
      document.body.appendChild( p );
      document.body.appendChild( p );

      p = document.createElement( "p" );
      p.textContent = "number : " + _isObj( 1 );
      document.body.appendChild( p );
      document.body.appendChild( p );

      p = document.createElement( "p" );
      p.textContent = "object : " + _isObj( {} );
      document.body.appendChild( p );

      p = document.createElement( "p" );
      p.textContent = "another object : " + _isObj( p );
      document.body.appendChild( p );





      hope this help






      share|improve this answer





















      • _isObj(new Buffer(1)) // true
        – Mirek Rusin
        Dec 6 '17 at 22:02










      • @Mirek why you test "new Buffer(1)" , scenario ?
        – Leonardo Ciaccio
        Mar 4 at 15:32


















      up vote
      0
      down vote













      Strangely, I'm seeing different values for toString() for a subclassed object depending how toString() is being called:



      Object.prototype.toString.call(aThing)
      "[object Object]"

      aThing.toString()
      "ZmPopupMenu"


      That results in a false positive, so I amended it to prefer the object's toString():



      var str = someObject.toString
      ? someObject.toString()
      : Object.prototype.toString.call(someObject);
      return str === '[object Object]';





      share|improve this answer























      • If an objects toString property is overridden by a function returning a different value, alert, for example, will display that value. However, Object.prototype.toString.call( anObject ) will always return the string [object Object] if anObject indeed is an object. To have Object.prototype.toString return something different, the Object’s prototype toString must itself be overridden.
        – Frederik Krautwald
        May 12 '15 at 21:05




















      up vote
      -2
      down vote













      This works for me:



      function isObject(o) {
      try {
      return ((typeof o == "object") && (o !== null) && (o.length === undefined));
      } catch (err) {
      return false;
      }
      }





      share|improve this answer





















        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4320767%2fcheck-that-value-is-object-literal%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        9 Answers
        9






        active

        oldest

        votes








        9 Answers
        9






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        37
        down vote













        This should do it for you:



        if( Object.prototype.toString.call( someObject ) === '[object Object]' ) {
        // do your iteration
        }


        From ECMAScript 5 Section 8.6.2 if you're interested:




        The value of the [[Class]] internal property is defined by this specification for every kind of built-in object. The value of the [[Class]] internal property of a host object may be any String value except one of "Arguments", "Array", "Boolean", "Date", "Error", "Function", "JSON", "Math", "Number", "Object", "RegExp", and "String". The value of a [[Class]] internal property is used internally to distinguish different kinds of objects. Note that this specification does not provide any means for a program to access that value except through Object.prototype.toString (see 15.2.4.2).







        share|improve this answer

















        • 3




          This is no longer guaranteed to work in ES6, as an object can change the value of its Symbol.toStringTag property, which is used to generate Object.prototype.toString's return value. See the note at: people.mozilla.org/~jorendorff/… It looks like the way to check if something's iterable now is to see if it's Symbol.iterator property is defined
          – Ethan
          Mar 18 '15 at 9:48










        • Object.prototype.toString could be overridden; thus, not safe.
          – Frederik Krautwald
          May 12 '15 at 21:11






        • 5




          If its ever overwritten; that dev should be shot! Any prototypical structure can have its methods overwritten, and in turn would be useless if you could not depend on them.
          – Josh Mc
          Aug 5 '15 at 2:01






        • 1




          for the ppl who still using to this answer be careful : var str; toString.call( str) if the value is undefined or null it will comeback "object" in IE < 9 and some mobiles . checkout first for undefined and null before you apply this condition .
          – SUB-HDR
          Feb 26 at 21:30















        up vote
        37
        down vote













        This should do it for you:



        if( Object.prototype.toString.call( someObject ) === '[object Object]' ) {
        // do your iteration
        }


        From ECMAScript 5 Section 8.6.2 if you're interested:




        The value of the [[Class]] internal property is defined by this specification for every kind of built-in object. The value of the [[Class]] internal property of a host object may be any String value except one of "Arguments", "Array", "Boolean", "Date", "Error", "Function", "JSON", "Math", "Number", "Object", "RegExp", and "String". The value of a [[Class]] internal property is used internally to distinguish different kinds of objects. Note that this specification does not provide any means for a program to access that value except through Object.prototype.toString (see 15.2.4.2).







        share|improve this answer

















        • 3




          This is no longer guaranteed to work in ES6, as an object can change the value of its Symbol.toStringTag property, which is used to generate Object.prototype.toString's return value. See the note at: people.mozilla.org/~jorendorff/… It looks like the way to check if something's iterable now is to see if it's Symbol.iterator property is defined
          – Ethan
          Mar 18 '15 at 9:48










        • Object.prototype.toString could be overridden; thus, not safe.
          – Frederik Krautwald
          May 12 '15 at 21:11






        • 5




          If its ever overwritten; that dev should be shot! Any prototypical structure can have its methods overwritten, and in turn would be useless if you could not depend on them.
          – Josh Mc
          Aug 5 '15 at 2:01






        • 1




          for the ppl who still using to this answer be careful : var str; toString.call( str) if the value is undefined or null it will comeback "object" in IE < 9 and some mobiles . checkout first for undefined and null before you apply this condition .
          – SUB-HDR
          Feb 26 at 21:30













        up vote
        37
        down vote










        up vote
        37
        down vote









        This should do it for you:



        if( Object.prototype.toString.call( someObject ) === '[object Object]' ) {
        // do your iteration
        }


        From ECMAScript 5 Section 8.6.2 if you're interested:




        The value of the [[Class]] internal property is defined by this specification for every kind of built-in object. The value of the [[Class]] internal property of a host object may be any String value except one of "Arguments", "Array", "Boolean", "Date", "Error", "Function", "JSON", "Math", "Number", "Object", "RegExp", and "String". The value of a [[Class]] internal property is used internally to distinguish different kinds of objects. Note that this specification does not provide any means for a program to access that value except through Object.prototype.toString (see 15.2.4.2).







        share|improve this answer












        This should do it for you:



        if( Object.prototype.toString.call( someObject ) === '[object Object]' ) {
        // do your iteration
        }


        From ECMAScript 5 Section 8.6.2 if you're interested:




        The value of the [[Class]] internal property is defined by this specification for every kind of built-in object. The value of the [[Class]] internal property of a host object may be any String value except one of "Arguments", "Array", "Boolean", "Date", "Error", "Function", "JSON", "Math", "Number", "Object", "RegExp", and "String". The value of a [[Class]] internal property is used internally to distinguish different kinds of objects. Note that this specification does not provide any means for a program to access that value except through Object.prototype.toString (see 15.2.4.2).








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 1 '10 at 2:53









        user113716

        258k55399416




        258k55399416








        • 3




          This is no longer guaranteed to work in ES6, as an object can change the value of its Symbol.toStringTag property, which is used to generate Object.prototype.toString's return value. See the note at: people.mozilla.org/~jorendorff/… It looks like the way to check if something's iterable now is to see if it's Symbol.iterator property is defined
          – Ethan
          Mar 18 '15 at 9:48










        • Object.prototype.toString could be overridden; thus, not safe.
          – Frederik Krautwald
          May 12 '15 at 21:11






        • 5




          If its ever overwritten; that dev should be shot! Any prototypical structure can have its methods overwritten, and in turn would be useless if you could not depend on them.
          – Josh Mc
          Aug 5 '15 at 2:01






        • 1




          for the ppl who still using to this answer be careful : var str; toString.call( str) if the value is undefined or null it will comeback "object" in IE < 9 and some mobiles . checkout first for undefined and null before you apply this condition .
          – SUB-HDR
          Feb 26 at 21:30














        • 3




          This is no longer guaranteed to work in ES6, as an object can change the value of its Symbol.toStringTag property, which is used to generate Object.prototype.toString's return value. See the note at: people.mozilla.org/~jorendorff/… It looks like the way to check if something's iterable now is to see if it's Symbol.iterator property is defined
          – Ethan
          Mar 18 '15 at 9:48










        • Object.prototype.toString could be overridden; thus, not safe.
          – Frederik Krautwald
          May 12 '15 at 21:11






        • 5




          If its ever overwritten; that dev should be shot! Any prototypical structure can have its methods overwritten, and in turn would be useless if you could not depend on them.
          – Josh Mc
          Aug 5 '15 at 2:01






        • 1




          for the ppl who still using to this answer be careful : var str; toString.call( str) if the value is undefined or null it will comeback "object" in IE < 9 and some mobiles . checkout first for undefined and null before you apply this condition .
          – SUB-HDR
          Feb 26 at 21:30








        3




        3




        This is no longer guaranteed to work in ES6, as an object can change the value of its Symbol.toStringTag property, which is used to generate Object.prototype.toString's return value. See the note at: people.mozilla.org/~jorendorff/… It looks like the way to check if something's iterable now is to see if it's Symbol.iterator property is defined
        – Ethan
        Mar 18 '15 at 9:48




        This is no longer guaranteed to work in ES6, as an object can change the value of its Symbol.toStringTag property, which is used to generate Object.prototype.toString's return value. See the note at: people.mozilla.org/~jorendorff/… It looks like the way to check if something's iterable now is to see if it's Symbol.iterator property is defined
        – Ethan
        Mar 18 '15 at 9:48












        Object.prototype.toString could be overridden; thus, not safe.
        – Frederik Krautwald
        May 12 '15 at 21:11




        Object.prototype.toString could be overridden; thus, not safe.
        – Frederik Krautwald
        May 12 '15 at 21:11




        5




        5




        If its ever overwritten; that dev should be shot! Any prototypical structure can have its methods overwritten, and in turn would be useless if you could not depend on them.
        – Josh Mc
        Aug 5 '15 at 2:01




        If its ever overwritten; that dev should be shot! Any prototypical structure can have its methods overwritten, and in turn would be useless if you could not depend on them.
        – Josh Mc
        Aug 5 '15 at 2:01




        1




        1




        for the ppl who still using to this answer be careful : var str; toString.call( str) if the value is undefined or null it will comeback "object" in IE < 9 and some mobiles . checkout first for undefined and null before you apply this condition .
        – SUB-HDR
        Feb 26 at 21:30




        for the ppl who still using to this answer be careful : var str; toString.call( str) if the value is undefined or null it will comeback "object" in IE < 9 and some mobiles . checkout first for undefined and null before you apply this condition .
        – SUB-HDR
        Feb 26 at 21:30












        up vote
        6
        down vote













        You could also do something like:



            if (someObject.constructor == Object) {
        // do your thing
        }


        you can read more about it here






        share|improve this answer

















        • 2




          If someObject is null, it doesn't have a constructor and therefore this throws a TypeError. You really want the first answer above.
          – chilts
          Jun 13 '13 at 3:19










        • This is far more elegant than the accepted answer for situations where you're trying to distinguish between arrays and objects in js.
          – ekillaby
          Jul 10 '13 at 18:44










        • @countfloortiles: yes and no - someObject.constructor = Object and the test will return true from there on.
          – CoDEmanX
          Dec 5 '14 at 23:39












        • @CoDEmanX, I don't understand what you mean.
          – ekillaby
          Dec 6 '14 at 0:23










        • @countfloortiles: .constructor can be overridden, so using it for type checking is not 100% safe.
          – CoDEmanX
          Dec 6 '14 at 14:27















        up vote
        6
        down vote













        You could also do something like:



            if (someObject.constructor == Object) {
        // do your thing
        }


        you can read more about it here






        share|improve this answer

















        • 2




          If someObject is null, it doesn't have a constructor and therefore this throws a TypeError. You really want the first answer above.
          – chilts
          Jun 13 '13 at 3:19










        • This is far more elegant than the accepted answer for situations where you're trying to distinguish between arrays and objects in js.
          – ekillaby
          Jul 10 '13 at 18:44










        • @countfloortiles: yes and no - someObject.constructor = Object and the test will return true from there on.
          – CoDEmanX
          Dec 5 '14 at 23:39












        • @CoDEmanX, I don't understand what you mean.
          – ekillaby
          Dec 6 '14 at 0:23










        • @countfloortiles: .constructor can be overridden, so using it for type checking is not 100% safe.
          – CoDEmanX
          Dec 6 '14 at 14:27













        up vote
        6
        down vote










        up vote
        6
        down vote









        You could also do something like:



            if (someObject.constructor == Object) {
        // do your thing
        }


        you can read more about it here






        share|improve this answer












        You could also do something like:



            if (someObject.constructor == Object) {
        // do your thing
        }


        you can read more about it here







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 1 '10 at 3:27









        dcestari

        40437




        40437








        • 2




          If someObject is null, it doesn't have a constructor and therefore this throws a TypeError. You really want the first answer above.
          – chilts
          Jun 13 '13 at 3:19










        • This is far more elegant than the accepted answer for situations where you're trying to distinguish between arrays and objects in js.
          – ekillaby
          Jul 10 '13 at 18:44










        • @countfloortiles: yes and no - someObject.constructor = Object and the test will return true from there on.
          – CoDEmanX
          Dec 5 '14 at 23:39












        • @CoDEmanX, I don't understand what you mean.
          – ekillaby
          Dec 6 '14 at 0:23










        • @countfloortiles: .constructor can be overridden, so using it for type checking is not 100% safe.
          – CoDEmanX
          Dec 6 '14 at 14:27














        • 2




          If someObject is null, it doesn't have a constructor and therefore this throws a TypeError. You really want the first answer above.
          – chilts
          Jun 13 '13 at 3:19










        • This is far more elegant than the accepted answer for situations where you're trying to distinguish between arrays and objects in js.
          – ekillaby
          Jul 10 '13 at 18:44










        • @countfloortiles: yes and no - someObject.constructor = Object and the test will return true from there on.
          – CoDEmanX
          Dec 5 '14 at 23:39












        • @CoDEmanX, I don't understand what you mean.
          – ekillaby
          Dec 6 '14 at 0:23










        • @countfloortiles: .constructor can be overridden, so using it for type checking is not 100% safe.
          – CoDEmanX
          Dec 6 '14 at 14:27








        2




        2




        If someObject is null, it doesn't have a constructor and therefore this throws a TypeError. You really want the first answer above.
        – chilts
        Jun 13 '13 at 3:19




        If someObject is null, it doesn't have a constructor and therefore this throws a TypeError. You really want the first answer above.
        – chilts
        Jun 13 '13 at 3:19












        This is far more elegant than the accepted answer for situations where you're trying to distinguish between arrays and objects in js.
        – ekillaby
        Jul 10 '13 at 18:44




        This is far more elegant than the accepted answer for situations where you're trying to distinguish between arrays and objects in js.
        – ekillaby
        Jul 10 '13 at 18:44












        @countfloortiles: yes and no - someObject.constructor = Object and the test will return true from there on.
        – CoDEmanX
        Dec 5 '14 at 23:39






        @countfloortiles: yes and no - someObject.constructor = Object and the test will return true from there on.
        – CoDEmanX
        Dec 5 '14 at 23:39














        @CoDEmanX, I don't understand what you mean.
        – ekillaby
        Dec 6 '14 at 0:23




        @CoDEmanX, I don't understand what you mean.
        – ekillaby
        Dec 6 '14 at 0:23












        @countfloortiles: .constructor can be overridden, so using it for type checking is not 100% safe.
        – CoDEmanX
        Dec 6 '14 at 14:27




        @countfloortiles: .constructor can be overridden, so using it for type checking is not 100% safe.
        – CoDEmanX
        Dec 6 '14 at 14:27










        up vote
        3
        down vote













        I think a function like this should be native, just like Array.isArray:



        Object.isObject = function(obj) {
        return obj && obj.constructor === this || false;
        };


        This one doesn't make function calls nor string comparisons, and doesn't reference global objects (like Object), so it should be quite fast. I don't think this will used in performance intensive tasks though, but whatever.



        I'm not totally convinced about the name... maybe something like Object.isLiteral would be better.



        It can cause problem with host objects, though, but I don't have a test ready.






        share|improve this answer





















        • the || false part is pretty unnecessary.
          – user633183
          Jan 28 '14 at 16:21






        • 5




          @naomik It's not if you want the function to always return a boolean. If obj === null it'd return null instead. You can always go for return obj != null && object.constructor === this;.
          – MaxArt
          Jan 28 '14 at 21:04






        • 2




          The other way is return !!(obj && obj.constructor === this)
          – Aram Kocharyan
          Sep 9 '15 at 3:25















        up vote
        3
        down vote













        I think a function like this should be native, just like Array.isArray:



        Object.isObject = function(obj) {
        return obj && obj.constructor === this || false;
        };


        This one doesn't make function calls nor string comparisons, and doesn't reference global objects (like Object), so it should be quite fast. I don't think this will used in performance intensive tasks though, but whatever.



        I'm not totally convinced about the name... maybe something like Object.isLiteral would be better.



        It can cause problem with host objects, though, but I don't have a test ready.






        share|improve this answer





















        • the || false part is pretty unnecessary.
          – user633183
          Jan 28 '14 at 16:21






        • 5




          @naomik It's not if you want the function to always return a boolean. If obj === null it'd return null instead. You can always go for return obj != null && object.constructor === this;.
          – MaxArt
          Jan 28 '14 at 21:04






        • 2




          The other way is return !!(obj && obj.constructor === this)
          – Aram Kocharyan
          Sep 9 '15 at 3:25













        up vote
        3
        down vote










        up vote
        3
        down vote









        I think a function like this should be native, just like Array.isArray:



        Object.isObject = function(obj) {
        return obj && obj.constructor === this || false;
        };


        This one doesn't make function calls nor string comparisons, and doesn't reference global objects (like Object), so it should be quite fast. I don't think this will used in performance intensive tasks though, but whatever.



        I'm not totally convinced about the name... maybe something like Object.isLiteral would be better.



        It can cause problem with host objects, though, but I don't have a test ready.






        share|improve this answer












        I think a function like this should be native, just like Array.isArray:



        Object.isObject = function(obj) {
        return obj && obj.constructor === this || false;
        };


        This one doesn't make function calls nor string comparisons, and doesn't reference global objects (like Object), so it should be quite fast. I don't think this will used in performance intensive tasks though, but whatever.



        I'm not totally convinced about the name... maybe something like Object.isLiteral would be better.



        It can cause problem with host objects, though, but I don't have a test ready.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 28 '13 at 10:07









        MaxArt

        15.5k85570




        15.5k85570












        • the || false part is pretty unnecessary.
          – user633183
          Jan 28 '14 at 16:21






        • 5




          @naomik It's not if you want the function to always return a boolean. If obj === null it'd return null instead. You can always go for return obj != null && object.constructor === this;.
          – MaxArt
          Jan 28 '14 at 21:04






        • 2




          The other way is return !!(obj && obj.constructor === this)
          – Aram Kocharyan
          Sep 9 '15 at 3:25


















        • the || false part is pretty unnecessary.
          – user633183
          Jan 28 '14 at 16:21






        • 5




          @naomik It's not if you want the function to always return a boolean. If obj === null it'd return null instead. You can always go for return obj != null && object.constructor === this;.
          – MaxArt
          Jan 28 '14 at 21:04






        • 2




          The other way is return !!(obj && obj.constructor === this)
          – Aram Kocharyan
          Sep 9 '15 at 3:25
















        the || false part is pretty unnecessary.
        – user633183
        Jan 28 '14 at 16:21




        the || false part is pretty unnecessary.
        – user633183
        Jan 28 '14 at 16:21




        5




        5




        @naomik It's not if you want the function to always return a boolean. If obj === null it'd return null instead. You can always go for return obj != null && object.constructor === this;.
        – MaxArt
        Jan 28 '14 at 21:04




        @naomik It's not if you want the function to always return a boolean. If obj === null it'd return null instead. You can always go for return obj != null && object.constructor === this;.
        – MaxArt
        Jan 28 '14 at 21:04




        2




        2




        The other way is return !!(obj && obj.constructor === this)
        – Aram Kocharyan
        Sep 9 '15 at 3:25




        The other way is return !!(obj && obj.constructor === this)
        – Aram Kocharyan
        Sep 9 '15 at 3:25










        up vote
        2
        down vote













        Say you have some testvar and want to see if it is an object, but not an array or null (these are both of type Object). You can do the following



        testVar instanceof Object && !Array.isArray(testVar) && testVar !== null





        share|improve this answer

























          up vote
          2
          down vote













          Say you have some testvar and want to see if it is an object, but not an array or null (these are both of type Object). You can do the following



          testVar instanceof Object && !Array.isArray(testVar) && testVar !== null





          share|improve this answer























            up vote
            2
            down vote










            up vote
            2
            down vote









            Say you have some testvar and want to see if it is an object, but not an array or null (these are both of type Object). You can do the following



            testVar instanceof Object && !Array.isArray(testVar) && testVar !== null





            share|improve this answer












            Say you have some testvar and want to see if it is an object, but not an array or null (these are both of type Object). You can do the following



            testVar instanceof Object && !Array.isArray(testVar) && testVar !== null






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jun 14 '17 at 21:14









            Mark

            2,49811120




            2,49811120






















                up vote
                0
                down vote













                Well, you don't need to check everything by yourself or write your own codes, when there are good libraries such as Lodash and Underscore.



                In Lodash, you can easily check it by isPlainObject function, e.g.:



                _.isPlainObject({'a': 12});


                Check this page: https://lodash.com/docs#isPlainObject






                share|improve this answer





















                • Just to be clear, Underscore has no such function - it does have _.isObject but that does not check for a plain object / object literal (e.g. it returns true also for an array)
                  – JHH
                  Apr 18 at 7:07















                up vote
                0
                down vote













                Well, you don't need to check everything by yourself or write your own codes, when there are good libraries such as Lodash and Underscore.



                In Lodash, you can easily check it by isPlainObject function, e.g.:



                _.isPlainObject({'a': 12});


                Check this page: https://lodash.com/docs#isPlainObject






                share|improve this answer





















                • Just to be clear, Underscore has no such function - it does have _.isObject but that does not check for a plain object / object literal (e.g. it returns true also for an array)
                  – JHH
                  Apr 18 at 7:07













                up vote
                0
                down vote










                up vote
                0
                down vote









                Well, you don't need to check everything by yourself or write your own codes, when there are good libraries such as Lodash and Underscore.



                In Lodash, you can easily check it by isPlainObject function, e.g.:



                _.isPlainObject({'a': 12});


                Check this page: https://lodash.com/docs#isPlainObject






                share|improve this answer












                Well, you don't need to check everything by yourself or write your own codes, when there are good libraries such as Lodash and Underscore.



                In Lodash, you can easily check it by isPlainObject function, e.g.:



                _.isPlainObject({'a': 12});


                Check this page: https://lodash.com/docs#isPlainObject







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Feb 25 '16 at 18:13









                Ehsan

                796616




                796616












                • Just to be clear, Underscore has no such function - it does have _.isObject but that does not check for a plain object / object literal (e.g. it returns true also for an array)
                  – JHH
                  Apr 18 at 7:07


















                • Just to be clear, Underscore has no such function - it does have _.isObject but that does not check for a plain object / object literal (e.g. it returns true also for an array)
                  – JHH
                  Apr 18 at 7:07
















                Just to be clear, Underscore has no such function - it does have _.isObject but that does not check for a plain object / object literal (e.g. it returns true also for an array)
                – JHH
                Apr 18 at 7:07




                Just to be clear, Underscore has no such function - it does have _.isObject but that does not check for a plain object / object literal (e.g. it returns true also for an array)
                – JHH
                Apr 18 at 7:07










                up vote
                0
                down vote













                Bumping old thread, but it's still shows up in searches and people are even referencing it as duplicate for a new similar ones - and still the top-most answers here are far from being correct (sorry people, no offends).



                To check if variable is an object the following should be used:



                if (typeof variable === 'object') {
                // do something
                }


                Arrays are also objects, so this would be true for an array too.
                Moreover - null is a valid object as well, therefore the above will return true on null too.
                Personally, when really need to check if an expected variable is 'workable' object I'm always using this boringly repeated formula:



                if (variable && typeof variable === `object`) {
                // do something
                }


                Last but not least :) please please please, do yourself a favor and don't use any libraries for such a simple things. Javascript is a fastly evolving language having today much much more than yesterday, so fast that most of the libraries are not even fast enough to fetch up.
                Beside it, people who are working on the spec are doing a great job and mostly the native APIs are clean, correct, making perfect sense and coherent with the rest of the language.






                share|improve this answer

























                  up vote
                  0
                  down vote













                  Bumping old thread, but it's still shows up in searches and people are even referencing it as duplicate for a new similar ones - and still the top-most answers here are far from being correct (sorry people, no offends).



                  To check if variable is an object the following should be used:



                  if (typeof variable === 'object') {
                  // do something
                  }


                  Arrays are also objects, so this would be true for an array too.
                  Moreover - null is a valid object as well, therefore the above will return true on null too.
                  Personally, when really need to check if an expected variable is 'workable' object I'm always using this boringly repeated formula:



                  if (variable && typeof variable === `object`) {
                  // do something
                  }


                  Last but not least :) please please please, do yourself a favor and don't use any libraries for such a simple things. Javascript is a fastly evolving language having today much much more than yesterday, so fast that most of the libraries are not even fast enough to fetch up.
                  Beside it, people who are working on the spec are doing a great job and mostly the native APIs are clean, correct, making perfect sense and coherent with the rest of the language.






                  share|improve this answer























                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    Bumping old thread, but it's still shows up in searches and people are even referencing it as duplicate for a new similar ones - and still the top-most answers here are far from being correct (sorry people, no offends).



                    To check if variable is an object the following should be used:



                    if (typeof variable === 'object') {
                    // do something
                    }


                    Arrays are also objects, so this would be true for an array too.
                    Moreover - null is a valid object as well, therefore the above will return true on null too.
                    Personally, when really need to check if an expected variable is 'workable' object I'm always using this boringly repeated formula:



                    if (variable && typeof variable === `object`) {
                    // do something
                    }


                    Last but not least :) please please please, do yourself a favor and don't use any libraries for such a simple things. Javascript is a fastly evolving language having today much much more than yesterday, so fast that most of the libraries are not even fast enough to fetch up.
                    Beside it, people who are working on the spec are doing a great job and mostly the native APIs are clean, correct, making perfect sense and coherent with the rest of the language.






                    share|improve this answer












                    Bumping old thread, but it's still shows up in searches and people are even referencing it as duplicate for a new similar ones - and still the top-most answers here are far from being correct (sorry people, no offends).



                    To check if variable is an object the following should be used:



                    if (typeof variable === 'object') {
                    // do something
                    }


                    Arrays are also objects, so this would be true for an array too.
                    Moreover - null is a valid object as well, therefore the above will return true on null too.
                    Personally, when really need to check if an expected variable is 'workable' object I'm always using this boringly repeated formula:



                    if (variable && typeof variable === `object`) {
                    // do something
                    }


                    Last but not least :) please please please, do yourself a favor and don't use any libraries for such a simple things. Javascript is a fastly evolving language having today much much more than yesterday, so fast that most of the libraries are not even fast enough to fetch up.
                    Beside it, people who are working on the spec are doing a great job and mostly the native APIs are clean, correct, making perfect sense and coherent with the rest of the language.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Apr 7 '17 at 6:33









                    GullerYA

                    276310




                    276310






















                        up vote
                        0
                        down vote













                        Little gist, not really elegant but efficient



                        function _isObj( _obj ){

                        return ( typeof _obj === "object" && JSON.stringify( _obj ).indexOf( "{" ) == 0 );

                        }


                        Little example






                        function _isObj( _obj ){

                        return ( typeof _obj === "object" && JSON.stringify( _obj ).indexOf( "{" ) == 0 );

                        }

                        var p = document.createElement( "p" );
                        p.textContent = "undefined : " + _isObj( undefined );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "null : " + _isObj( null );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "boolean : " + _isObj( true );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "function : " + _isObj( function(){} );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "array : " + _isObj( );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "string : " + _isObj( "{}" );
                        document.body.appendChild( p );
                        document.body.appendChild( p );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "number : " + _isObj( 1 );
                        document.body.appendChild( p );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "object : " + _isObj( {} );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "another object : " + _isObj( p );
                        document.body.appendChild( p );





                        hope this help






                        share|improve this answer





















                        • _isObj(new Buffer(1)) // true
                          – Mirek Rusin
                          Dec 6 '17 at 22:02










                        • @Mirek why you test "new Buffer(1)" , scenario ?
                          – Leonardo Ciaccio
                          Mar 4 at 15:32















                        up vote
                        0
                        down vote













                        Little gist, not really elegant but efficient



                        function _isObj( _obj ){

                        return ( typeof _obj === "object" && JSON.stringify( _obj ).indexOf( "{" ) == 0 );

                        }


                        Little example






                        function _isObj( _obj ){

                        return ( typeof _obj === "object" && JSON.stringify( _obj ).indexOf( "{" ) == 0 );

                        }

                        var p = document.createElement( "p" );
                        p.textContent = "undefined : " + _isObj( undefined );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "null : " + _isObj( null );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "boolean : " + _isObj( true );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "function : " + _isObj( function(){} );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "array : " + _isObj( );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "string : " + _isObj( "{}" );
                        document.body.appendChild( p );
                        document.body.appendChild( p );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "number : " + _isObj( 1 );
                        document.body.appendChild( p );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "object : " + _isObj( {} );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "another object : " + _isObj( p );
                        document.body.appendChild( p );





                        hope this help






                        share|improve this answer





















                        • _isObj(new Buffer(1)) // true
                          – Mirek Rusin
                          Dec 6 '17 at 22:02










                        • @Mirek why you test "new Buffer(1)" , scenario ?
                          – Leonardo Ciaccio
                          Mar 4 at 15:32













                        up vote
                        0
                        down vote










                        up vote
                        0
                        down vote









                        Little gist, not really elegant but efficient



                        function _isObj( _obj ){

                        return ( typeof _obj === "object" && JSON.stringify( _obj ).indexOf( "{" ) == 0 );

                        }


                        Little example






                        function _isObj( _obj ){

                        return ( typeof _obj === "object" && JSON.stringify( _obj ).indexOf( "{" ) == 0 );

                        }

                        var p = document.createElement( "p" );
                        p.textContent = "undefined : " + _isObj( undefined );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "null : " + _isObj( null );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "boolean : " + _isObj( true );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "function : " + _isObj( function(){} );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "array : " + _isObj( );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "string : " + _isObj( "{}" );
                        document.body.appendChild( p );
                        document.body.appendChild( p );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "number : " + _isObj( 1 );
                        document.body.appendChild( p );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "object : " + _isObj( {} );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "another object : " + _isObj( p );
                        document.body.appendChild( p );





                        hope this help






                        share|improve this answer












                        Little gist, not really elegant but efficient



                        function _isObj( _obj ){

                        return ( typeof _obj === "object" && JSON.stringify( _obj ).indexOf( "{" ) == 0 );

                        }


                        Little example






                        function _isObj( _obj ){

                        return ( typeof _obj === "object" && JSON.stringify( _obj ).indexOf( "{" ) == 0 );

                        }

                        var p = document.createElement( "p" );
                        p.textContent = "undefined : " + _isObj( undefined );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "null : " + _isObj( null );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "boolean : " + _isObj( true );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "function : " + _isObj( function(){} );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "array : " + _isObj( );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "string : " + _isObj( "{}" );
                        document.body.appendChild( p );
                        document.body.appendChild( p );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "number : " + _isObj( 1 );
                        document.body.appendChild( p );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "object : " + _isObj( {} );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "another object : " + _isObj( p );
                        document.body.appendChild( p );





                        hope this help






                        function _isObj( _obj ){

                        return ( typeof _obj === "object" && JSON.stringify( _obj ).indexOf( "{" ) == 0 );

                        }

                        var p = document.createElement( "p" );
                        p.textContent = "undefined : " + _isObj( undefined );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "null : " + _isObj( null );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "boolean : " + _isObj( true );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "function : " + _isObj( function(){} );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "array : " + _isObj( );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "string : " + _isObj( "{}" );
                        document.body.appendChild( p );
                        document.body.appendChild( p );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "number : " + _isObj( 1 );
                        document.body.appendChild( p );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "object : " + _isObj( {} );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "another object : " + _isObj( p );
                        document.body.appendChild( p );





                        function _isObj( _obj ){

                        return ( typeof _obj === "object" && JSON.stringify( _obj ).indexOf( "{" ) == 0 );

                        }

                        var p = document.createElement( "p" );
                        p.textContent = "undefined : " + _isObj( undefined );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "null : " + _isObj( null );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "boolean : " + _isObj( true );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "function : " + _isObj( function(){} );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "array : " + _isObj( );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "string : " + _isObj( "{}" );
                        document.body.appendChild( p );
                        document.body.appendChild( p );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "number : " + _isObj( 1 );
                        document.body.appendChild( p );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "object : " + _isObj( {} );
                        document.body.appendChild( p );

                        p = document.createElement( "p" );
                        p.textContent = "another object : " + _isObj( p );
                        document.body.appendChild( p );






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Sep 15 '17 at 7:35









                        Leonardo Ciaccio

                        723711




                        723711












                        • _isObj(new Buffer(1)) // true
                          – Mirek Rusin
                          Dec 6 '17 at 22:02










                        • @Mirek why you test "new Buffer(1)" , scenario ?
                          – Leonardo Ciaccio
                          Mar 4 at 15:32


















                        • _isObj(new Buffer(1)) // true
                          – Mirek Rusin
                          Dec 6 '17 at 22:02










                        • @Mirek why you test "new Buffer(1)" , scenario ?
                          – Leonardo Ciaccio
                          Mar 4 at 15:32
















                        _isObj(new Buffer(1)) // true
                        – Mirek Rusin
                        Dec 6 '17 at 22:02




                        _isObj(new Buffer(1)) // true
                        – Mirek Rusin
                        Dec 6 '17 at 22:02












                        @Mirek why you test "new Buffer(1)" , scenario ?
                        – Leonardo Ciaccio
                        Mar 4 at 15:32




                        @Mirek why you test "new Buffer(1)" , scenario ?
                        – Leonardo Ciaccio
                        Mar 4 at 15:32










                        up vote
                        0
                        down vote













                        Strangely, I'm seeing different values for toString() for a subclassed object depending how toString() is being called:



                        Object.prototype.toString.call(aThing)
                        "[object Object]"

                        aThing.toString()
                        "ZmPopupMenu"


                        That results in a false positive, so I amended it to prefer the object's toString():



                        var str = someObject.toString
                        ? someObject.toString()
                        : Object.prototype.toString.call(someObject);
                        return str === '[object Object]';





                        share|improve this answer























                        • If an objects toString property is overridden by a function returning a different value, alert, for example, will display that value. However, Object.prototype.toString.call( anObject ) will always return the string [object Object] if anObject indeed is an object. To have Object.prototype.toString return something different, the Object’s prototype toString must itself be overridden.
                          – Frederik Krautwald
                          May 12 '15 at 21:05

















                        up vote
                        0
                        down vote













                        Strangely, I'm seeing different values for toString() for a subclassed object depending how toString() is being called:



                        Object.prototype.toString.call(aThing)
                        "[object Object]"

                        aThing.toString()
                        "ZmPopupMenu"


                        That results in a false positive, so I amended it to prefer the object's toString():



                        var str = someObject.toString
                        ? someObject.toString()
                        : Object.prototype.toString.call(someObject);
                        return str === '[object Object]';





                        share|improve this answer























                        • If an objects toString property is overridden by a function returning a different value, alert, for example, will display that value. However, Object.prototype.toString.call( anObject ) will always return the string [object Object] if anObject indeed is an object. To have Object.prototype.toString return something different, the Object’s prototype toString must itself be overridden.
                          – Frederik Krautwald
                          May 12 '15 at 21:05















                        up vote
                        0
                        down vote










                        up vote
                        0
                        down vote









                        Strangely, I'm seeing different values for toString() for a subclassed object depending how toString() is being called:



                        Object.prototype.toString.call(aThing)
                        "[object Object]"

                        aThing.toString()
                        "ZmPopupMenu"


                        That results in a false positive, so I amended it to prefer the object's toString():



                        var str = someObject.toString
                        ? someObject.toString()
                        : Object.prototype.toString.call(someObject);
                        return str === '[object Object]';





                        share|improve this answer














                        Strangely, I'm seeing different values for toString() for a subclassed object depending how toString() is being called:



                        Object.prototype.toString.call(aThing)
                        "[object Object]"

                        aThing.toString()
                        "ZmPopupMenu"


                        That results in a false positive, so I amended it to prefer the object's toString():



                        var str = someObject.toString
                        ? someObject.toString()
                        : Object.prototype.toString.call(someObject);
                        return str === '[object Object]';






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Aug 16 at 16:54









                        Mike Samuel

                        92.3k23169213




                        92.3k23169213










                        answered Feb 3 '15 at 19:16









                        Conrad Damon

                        49153




                        49153












                        • If an objects toString property is overridden by a function returning a different value, alert, for example, will display that value. However, Object.prototype.toString.call( anObject ) will always return the string [object Object] if anObject indeed is an object. To have Object.prototype.toString return something different, the Object’s prototype toString must itself be overridden.
                          – Frederik Krautwald
                          May 12 '15 at 21:05




















                        • If an objects toString property is overridden by a function returning a different value, alert, for example, will display that value. However, Object.prototype.toString.call( anObject ) will always return the string [object Object] if anObject indeed is an object. To have Object.prototype.toString return something different, the Object’s prototype toString must itself be overridden.
                          – Frederik Krautwald
                          May 12 '15 at 21:05


















                        If an objects toString property is overridden by a function returning a different value, alert, for example, will display that value. However, Object.prototype.toString.call( anObject ) will always return the string [object Object] if anObject indeed is an object. To have Object.prototype.toString return something different, the Object’s prototype toString must itself be overridden.
                        – Frederik Krautwald
                        May 12 '15 at 21:05






                        If an objects toString property is overridden by a function returning a different value, alert, for example, will display that value. However, Object.prototype.toString.call( anObject ) will always return the string [object Object] if anObject indeed is an object. To have Object.prototype.toString return something different, the Object’s prototype toString must itself be overridden.
                        – Frederik Krautwald
                        May 12 '15 at 21:05












                        up vote
                        -2
                        down vote













                        This works for me:



                        function isObject(o) {
                        try {
                        return ((typeof o == "object") && (o !== null) && (o.length === undefined));
                        } catch (err) {
                        return false;
                        }
                        }





                        share|improve this answer

























                          up vote
                          -2
                          down vote













                          This works for me:



                          function isObject(o) {
                          try {
                          return ((typeof o == "object") && (o !== null) && (o.length === undefined));
                          } catch (err) {
                          return false;
                          }
                          }





                          share|improve this answer























                            up vote
                            -2
                            down vote










                            up vote
                            -2
                            down vote









                            This works for me:



                            function isObject(o) {
                            try {
                            return ((typeof o == "object") && (o !== null) && (o.length === undefined));
                            } catch (err) {
                            return false;
                            }
                            }





                            share|improve this answer












                            This works for me:



                            function isObject(o) {
                            try {
                            return ((typeof o == "object") && (o !== null) && (o.length === undefined));
                            } catch (err) {
                            return false;
                            }
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jun 17 '13 at 2:00









                            NeonMonk

                            223




                            223






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4320767%2fcheck-that-value-is-object-literal%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown