+ 1

Strings equality in JavaScript

Why "string" === "string" is false in JavaScript.

29th Sep 2023, 10:11 AM
Umer Khan
Umer Khan - avatar
3 Answers
+ 4
// Test the code again console.log("string" === "string") // true // Do you mean new String("string")? console.log(new String("string") === new String("string")) // false, different objects
29th Sep 2023, 10:15 AM
Lisa
Lisa - avatar
+ 4
a and b are different objects. they are different instances of String(). === compares for identity, not the content of the String objects.
29th Sep 2023, 10:29 AM
Lisa
Lisa - avatar
+ 2
var a = new String("hello"); var b = new String("hello"); if(a === b) { alert("a"); } else { alert("b"); } Why this is resulting in "b"?
29th Sep 2023, 10:26 AM
Umer Khan
Umer Khan - avatar