+ 2
Can any one?
What is the output of this code? if ("String" == ["String"] ) { const str = new String ("String"); console.log(str == String); } else console.log("exit");
1 Odpowiedź
+ 8
Type Coercion
https://www.sololearn.com/post/274762/?ref=app
When array ["String"] is compared with a string "String" with loose comparison operator, its toString function is invoked, so we are comparing "String" with "String", so the if condition is true.
Then, in the if code block, str is a string object "String" constructed with the String constructor, and String without quotation mark is a reserved word, pointing to the constructor function. When we compare an oject with a function, it returns false.
https://code.sololearn.com/WdnsoGEKFbMy/?ref=app
So the printed result is false.