Skip to content

Instantly share code, notes, and snippets.

@sorensen
Created April 1, 2014 23:02
Show Gist options
  • Save sorensen/9924872 to your computer and use it in GitHub Desktop.
Save sorensen/9924872 to your computer and use it in GitHub Desktop.
Find first defined argument
/**
* Get first defined argument
*
* @param {...} any argument(s)
* @return {Any} first defined argument
*/
exports.defined = function() {
var args = Array.prototype.slice.call(arguments);
for (var i = 0; i < args.length; i++) {
var arg = args[i]
, type = Object.prototype.toString.call(arg)
;
// Check that arg is not undefined, null, or NaN
if (type !== '[object Undefined]' && type !== '[object Null]' && !isNaN(arg)) {
return arg;
}
}
// Return last argument
return args[args.length - 1];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment