2.3 KiB
2.3 KiB
title |
---|
Comparison with the strict equality operator |
Comparison with the strict equality operator
Problem explanation:
· Use the strict equality operator in the if
statement so the function will return "Equal" when val
is strictly equal to 7
.
Hint 1
Remember from last exercise that equality is different from assignment (=
), which assigns the value at the right of the operator to a variable in the left.1
try to solve the problem now
Hint 2
Unlike the equality operator, which attempts to convert both values being compared to a common type, the strict equality operator does not perform a type conversion.2
try to solve the problem now
Spoiler alert!
Solution ahead!
Basic code solution:
// Setup
function testStrict(val) {
if (val === 7) { // Change this line
return "Equal";
}
return "Not equal";
}
// Change this value to test
testStrict(10);
Code explanation
The function first evaluates if
the condition (val === 7)
evaluates to true
. If it does, it returns the statement between the curly braces ("Equal"). If it doesn't, it returns the next return
statement outside them ("Not equal").