+ 2

Explain the output

var x=new String ("hello"); var y=new String ("hello"); if(x===y) {alert("a"); } else if (x==y) {alert("b"); } Why is it alerting b? Ain't x & y identical/equivalent ?

20th May 2020, 4:02 AM
Kajal Kumari
Kajal Kumari - avatar
1 Answer
0
I tried var x = new String("hello"); var y = new String("hello"); if(x.localeCompare(y)==0) alert("a"); else if(x.localeCompare(y) == 0) alert("b"); and it works fine But when I ran your code, it doesn't alert anything, because when two string objects are compared using '==' operator JS returns false https://code.sololearn.com/WqQUHXHKgc7G/?ref=app
20th May 2020, 4:11 AM
Abhishek Tandon
Abhishek Tandon - avatar