Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. Javascript object comparison

Javascript object comparison

Scheduled Pinned Locked Moved JavaScript
javascriptquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anandkumar Prajapati
    wrote on last edited by
    #1

    $(document).ready(function(){
    var obj1 = { }
    var obj2 = { }
    if(obj1 === obj2)
    {
    alert('true');
    }
    else
    {
    alert('False');
    }
    });

    Why if(obj1 == obj2) returns false eventhough obj1 and obj2 are objects of same type?

    L 1 Reply Last reply
    0
    • A Anandkumar Prajapati

      $(document).ready(function(){
      var obj1 = { }
      var obj2 = { }
      if(obj1 === obj2)
      {
      alert('true');
      }
      else
      {
      alert('False');
      }
      });

      Why if(obj1 == obj2) returns false eventhough obj1 and obj2 are objects of same type?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Because the comparison is to see if they are references to the same object. And as you see they are two different objects.

      A 1 Reply Last reply
      0
      • L Lost User

        Because the comparison is to see if they are references to the same object. And as you see they are two different objects.

        A Offline
        A Offline
        A_Griffin
        wrote on last edited by
        #3

        OK, but then why then does this return True?

        var obj1 = 2;
        var obj2 = 2;
        if(obj1 === obj2){
        alert('true');
        } else {
        alert('False');
        }

        It's the same code but for assigning an integer to the obj variables rather than an empty JSON string.

        L 1 Reply Last reply
        0
        • A A_Griffin

          OK, but then why then does this return True?

          var obj1 = 2;
          var obj2 = 2;
          if(obj1 === obj2){
          alert('true');
          } else {
          alert('False');
          }

          It's the same code but for assigning an integer to the obj variables rather than an empty JSON string.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Because the value 2 is a constant so the references will point to the same item.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups