First, expanding an existing environment variable works, eg:
var homeVar = expand("$HOME");
log("HOME = '" + homeVar + "'");
This prints my home directory.
However, when the env var does not exist, I get what to me is unexpected behaviour:
var testVar = expand("$NONEXISTENT_VAR");
log("NONEXISTENT_VAR = '" + testVar + "'");
Expected
testVar is empty, or undefined.
Observed
testVar is a string with value $NONEXISTENT_VAR. Ie, it seems to expand the non-existent variable to itself, or, not fail for a non-existent variable so it's left unaltered.