HTML DOM isSameNode Method |

This method checks whether two nodes are the same node in the DOM tree.

Syntax

node.isSameNode(otherNode)

Parameters

Parameter Description
otherNode The node to compare with the current node.

Return Value

Returns true if the two nodes are the same; otherwise, returns false.

Example

Demonstrates how to use the isSameNode() method to check if two elements refer to the same DOM node.

<!DOCTYPE html>
<html>
<body>

<div id="div1">This is a div element.</div>
<div id="div2">This is another div element.</div>

<script>
// Get references to the two div elements
var div1 = document.getElementById("div1");
var div2 = document.getElementById("div2");

// Check if they are the same node
if (div1.isSameNode(div2)) {
  console.log("The two nodes are the same.");
} else {
  console.log("The two nodes are different.");
}

// Create a reference to div1
var div1Copy = div1;

// Check again
if (div1.isSameNode(div1Copy)) {
  console.log("div1 and div1Copy refer to the same node.");
} else {
  console.log("div1 and div1Copy do not refer to the same node.");
}
</script>

</body>
</html>

Output

The two nodes are different.
div1 and div1Copy refer to the same node.

Browser Support

This method is supported in all major browsers.