0
var elem1, elem2;

// document.forms is an HTMLCollection

elem1 = document.forms[0];
elem2 = document.forms.item(0);

alert(elem1 === elem2); // shows: "true"

elem1 = document.forms["myForm"];
elem2 = document.forms.namedItem("myForm");

alert(elem1 === elem2); // shows: "true"

Src: https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection

In the above code variables elem1 and elem2 both hold same object, i.e. a DOM node

I would like to know, In the statement elem1 === elem2 what is actually being compared so that it evaluates to a TRUE expression. Is it nodeType, nodeValue or nodeName ?

dkjain
  • 831
  • 1
  • 9
  • 36

2 Answers2

4

None of the above.

The === operator checks for reference equality.
It will only ever return true if both expressions refer to the same object.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

No property is actually compared. elem1 and elem2 are references to objects, and it happens that both variables point to the same object.

You could have used == too, in this case. === additionally checks for the type of the argument.

MaxArt
  • 22,200
  • 10
  • 82
  • 81
  • Does that applies to the following var myCar= new car(); or var x=document.getElementById("para"); I mean myCar and x are references to objects ? – dkjain Oct 01 '13 at 16:05
  • @dkjain: All Javascript variables are references to objects (except primitive values) – SLaks Oct 01 '13 at 17:29