Open In App

Underscore.js _.firstExisting() Method

Last Updated : 07 Aug, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The _.firstExisting() method returns the first existing argument from the arguments list.

Note: If the first argument is not exist like null or undefined, this method ignores that argument and checks for next.

Syntax:

_.firstExisting(arg1, arg2,....,argn);

Parameters: This method takes n arguments to check for the first existing argument.

Return Value: This method returns the first existing arguments.

Note: This will not work in normal JavaScript because it requires the underscore.js contrib library to be installed. Underscore.js contrib library can be installed using npm install underscore-contrib –save.

Example 1: 

Javascript




// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
var f_exist = _.firstExisting("gfg", "abc", 0, 1);;
  
console.log("First Existing value is : ",f_exist);


Output:

First Existing value is :  gfg

Example 2: If first argument is non-existing then this method checks for next existing one.

Javascript




// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
var f_exist = _.firstExisting(null, undefined, "gfg", "abc", 0, 1);;
  
console.log("First Existing value is : ",f_exist);


Output:

First Existing value is :  gfg

Example 3: 

Javascript




// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
var f_exist = _.firstExisting( 1, 2, 2, 3, 3, 4 );;
  
console.log("First Existing value is : ",f_exist);


Output:

First Existing value is :  1


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads