Skip to content

Instantly share code, notes, and snippets.

@Error601
Created May 21, 2015 22:06
Show Gist options
  • Save Error601/11b0554a6ba7062c32e1 to your computer and use it in GitHub Desktop.
Save Error601/11b0554a6ba7062c32e1 to your computer and use it in GitHub Desktop.
Return first defined value from argument list.
/*!
* Return the first defined value from argument
* list. Vars must be declared before passing
* to the function - set equal to self to catch
* any existing value for that var name. Useful
* for fetching values from an outer scope that
* may or may not be defined yet, while providing
* a fallback of a known (explicitly) defined value.
*
* // vars declared and assigned to self
* var foo=foo, bar=bar;
* var name = firstDefined(foo, bar, 'Bob');
* // returns 'Bob' since foo and bar are undefined
*/
function firstDefined() {
var undefined, i = -1;
while (++i < arguments.length) {
if (arguments[i] !== undefined) {
return arguments[i];
}
}
return undefined;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment