Can someone please tell why this:
if (1 % 2 === 0) {
console.log(true);
} else {
console.log(false);
}
returns false and this:
var num = 1;
if (function(num) {
return num % 2 === 0;
}) {
console.log(true);
} else {
console.log(false);
}
returns true?
First, it’s a terrible idea to do something like this.
Second, here is the thing. You’re not calling the function, so you don’t get the return value of false passed to if block. It just gets a function object (SOMETHING LIKE THIS, please, correct me if you know how to better phrase this) and this is a truthy value. If you want it the function to execute to get your value you would have to wrap your function in (function() { // code block }) (), that would be immediately called function and it would return your false value as you want it to.
1 Like
The function STATEMENT never gets invoked in the second block. Knowing that, we also know that it never returns anything, so there’s no boolean value (true or false) to be tested in the if statement.
ok, that makes sense. it works now.