You can not only use functions in if in JavaScript, but you can do this in almost all programming languages. This case is specially highlighted for JavaScript, as functions are the first citizens in it. JavaScript has almost all functions. A function is an object, a function is an interface, a function is the return value of another function, a function can be a parameter, a function creates closures, etc. Therefore, it is 100% acceptable.
You can run this example in Firebug to see that it works.
var validator = function (input) { return Boolean(input); } if (validator('')) { alert('true is returned from function'); } if (validator('something')) { alert('true is returned from function'); }
Also, as a hint, why using comparison operators in if blocks when we know that the expression is a Boolean expression?
source share