Design & Download
Your JS Library
in Seconds!!!

Snippet: v1() - Determine 1st Non-nothing Value

Written by Christopher West (cwest) on July 07, 2016.
Gets the first non-null and non-undefined value if it exists.
function v1() {
  for (var args = arguments, i = 0, l = args.length; i < l && args[i] == undefined; i++);
  return has(args, i) ? args[i] : args[l - 1];
}

v1(...) API Documentation

Gets the first non-null and non-undefined value if it exists.

Parameters

  1. ...values {*}:
    The values to be searched.

Returns

The first non-null and non-undefined value in the values passed to the function.

Example

console.log(YourJS.v1(undefined, 4));  // -> 4
console.log(YourJS.v1(5, null));  // -> 5
console.log(YourJS.v1(undefined, null, '8'));  // -> '8'
console.log(YourJS.v1(null, 0, false, undefined));  // -> 0
console.log(YourJS.v1('1.d'));  // -> '1.d'
console.log(YourJS.v1());  // -> undefined