Predict the output of the following javascript code
let result = 5 == "5";
alert(result);
Quiz Explanation
In JavaScript, the loose equality operator (==
) compares two values after converting them to a common type.
In this case, 5
(a number) and "5"
(a string) are being compared.
Since the loose equality operator performs type coercion, the string "5"
will be coerced into a number before comparison.
Both "5"
and 5
represent the same numeric value, so the comparison 5 == 5
evaluates to true
.
Therefore, the variable result
will hold the value true
, and the alert()
function will display a dialog box with the message "true".