Matthew Flaschen
Matthew Flaschen

Reputation: 284836

Are JavaScript equality operators for DOM elements cross-browser?

Will == and === work correctly in all browsers for DOM elements? If the code gets a reference to a raw DOM element in two different ways, will they be both == and === equal in all browsers?

Upvotes: 6

Views: 201

Answers (1)

zzzzBov
zzzzBov

Reputation: 179116

Will == and === work correctly in all browsers for DOM elements?

Yes, those equality operators will work as defined by the ECMAScript standard.

One word of caution, == often does things that developers do not expect, such as casting to a string when compared to a string value. This would make the following statement true, although it might not be the desired result:

document.createElement('div') == '[object HTMLDivElement]'

In most cases, you'll want to use the === operator.

Upvotes: 5

Related Questions