The following syntax works perfectly fine in PHP:
if($data = check_database($conds)) var_dump($data);
else echo "Not found";
However, in Javascript, I can't find an equivalent shorthand:
/*THIS CREATES A GLOBAL VARIABLE:*/
if(data = check_database(conds)) console.log(data);
else console.log("Not found.");
/*THIS RESULTS IN A SYNTAX ERROR (unexpected token var):*/
if(var data = check_database(conds)) console.log(data);
else console.log("Not found.");
Is there a way for me to declare a locally-scoped var in Javascript within the if
(and other) statement(s). NOTE: This is not a problem for the for
statement which expects complete statements as arguments.
Ultimately, I want to avoid the following verbose syntax:
/*THIS HAS THE DESIRED EFFECT, BUT UNDESIRED (VERBOSE) SYNTAX*/
var data;
if(data = check_database(conds)) console.log(data);
else console.log("Not found.");