Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 372
Matthew Flaschen Avatar asked Jan 18 '13 21:01

Matthew Flaschen


People also ask

What is == operator in JavaScript?

The equality operator ( == ) checks whether its two operands are equal, returning a Boolean result. Unlike the strict equality operator, it attempts to convert and compare operands that are of different types.

What is === and == in JavaScript?

The strict equality operator ( === ) checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality operator always considers operands of different types to be different.

Which operator will return false if two values are equal?

inequality operator returns false if two values are equal to each other according to == and returns true otherwise. The !==


1 Answers

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.

like image 91
zzzzBov Avatar answered Sep 28 '22 01:09

zzzzBov