Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript (strict) equality of new String("a") objects

There is in the ECMAScript Language Specification 11.9.1 The Equals Operator (==):

NOTE 3 The equality operator is not always transitive. For example, there might be two distinct String objects, each representing the same String value; each String object would be considered equal to the String value by the == operator, but the two String objects would not be equal to each other. For Example:

  • new String("a") == "a" and "a" == new String("a") are both true.
  • new String("a") == new String("a") is false.

but in 11.9.6 The Strict Equality Comparison Algorithm:

7. Return true if x and y refer to the same object. Otherwise, return false.

But no, with jsc

new String("a")===new String("a")

is true.

Is that a jsc.exe error? Or are they the same object? Than why?

like image 262
Aaaaaaaa Avatar asked May 21 '26 13:05

Aaaaaaaa


1 Answers

The operator === in js return true if is equal type and equal value. So is correct that it return true because they are both String object and their value is "a".

Have a look at the documentation http://www.w3schools.com/js/js_comparisons.asp

like image 132
Matteo Corti Avatar answered May 23 '26 02:05

Matteo Corti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!