You cannot define a variable inside an if block. So the answer is no, you cannot do this in javascript. In PHP, a variable definition will return true , which will control if .
But the assignment operator returns true, so you can use:
var $variable; if($variable = 5) { alert($variable); }
source share