0

Why x == y, but x !== y in this script? JavaScript, DOM

<!DOCTYPE html> <html> <head> </head> <body> <p id="demo">text</p> <script> var x = document.getElementById("demo"); var y = "[object HTMLParagraphElement]"; if (x == y) { document.write("x == y"); } if (x === y) { document.write("x === y"); } </script> </body> </html> The output is: text x == y

3rd Nov 2017, 10:24 AM
Augustinas Lukauskas
Augustinas Lukauskas - avatar
4 Answers
+ 4
It's a node element... (HTMLParagraphElement, as returned in string format ^^)
3rd Nov 2017, 10:51 AM
visph
visph - avatar
+ 4
x and y are not of same type... you should compare: x.toString() == y
3rd Nov 2017, 10:29 AM
visph
visph - avatar
+ 1
Thank you!!!
3rd Nov 2017, 10:56 AM
Augustinas Lukauskas
Augustinas Lukauskas - avatar
0
Thank you visph. What data type is variable "x"?
3rd Nov 2017, 10:49 AM
Augustinas Lukauskas
Augustinas Lukauskas - avatar