I do not know whether this has a term for itself or not, but, in PHP I can do:
if ($variable = 5)
{
echo $variable; // 5
}
Where, the same for JS fails:
if (var variable = 5)
{
echo variable; // Unexpected token var
}
Is there an equivalent?
You cannot define a variable inside
ifblock. So, the answer is no, you cannot do this in javascript. In PHP, a variable definition will returntruewhich will drive theif.But, an assignment statement returns true, so you can use: