Why string = '0' is not strictly equal to the new String ('0') in javascript

Why doesn't creating a string object return true when comparing strictly with a primitive string value?

var obj = new String('0');
var str = '0';

console.log(obj == str); //returns true
console.log(obj === str); //returns false
+4
source share
3 answers

Like objtype object, where str- string, Therefore obj === str- false.

var obj = new String('0');
var str = '0';

console.log(typeof obj);
console.log(typeof str);
Run codeHide result
+9
source
console.log(obj == str); //returns true
console.log(obj === str); //returns false

- . , . /, . , str , obj , .

new String('xxx') == new String('xxx'), , , . . https://www.w3schools.com/js/js_type_conversion.asp.

0

String Object String type value - , '===' false

, .

var obj = new String('0'); // this is object type
var str = '0';             // this is string type value

'===', ,

Strict equality compares two values ​​for equality. Before matching, no value is converted to any other value. If the values ​​are of different types, the values ​​are considered unequal. More details

So when you use '===' (strictly compare), it returns false

Check this answer to see the difference between a string object and a string type value. The difference between a string and a javascript string object

0
source

Source: https://habr.com/ru/post/1681193/


All Articles