Can any one? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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");

31st Mar 2020, 3:11 AM
Keerthana Nevuri
Keerthana Nevuri - avatar
1 Answer
+ 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.
31st Mar 2020, 3:25 AM
Gordon
Gordon - avatar