Wednesday, November 19, 2025
HomeLanguagesJavascriptUnderscore.js _.hasPath() Method

Underscore.js _.hasPath() Method

The _.hasPath() method returns a boolean indicating whether there is a property at the path described by the keys given. Keys may be given as an array or as a dot-separated string.

Syntax:

_.hasPath( Object_name, key_string|array );

Parameters: 

  • Object_name: Object from which the value is to be searched.
  • key_string | array: Given dot-separated string or array to be searched in the given object.

Return Value: This method returns a Boolean.

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 ob = { gfg : {
neveropen :  "Computer Science Portal for Geeks"
            }
        };
var val = _.hasPath( ob, ['gfg','neveropen'] );
  
console.log(val);


Output:

true

Example 2: 

Javascript




// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
var ob = { gfg : {
    neveropen :  "Computer Science Portal for Geeks"
            }
        };
var val = _.hasPath( ob, 'gfg.neveropen');
  
console.log(val);


Output:

true

Example 3: 

Javascript




// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
var ob = { gfg : {
neveropen :  "Computer Science Portal for Geeks"
            }
        };
var val = _.hasPath( ob, 'gfg.Geeks');
  
console.log(val);


Output:

false
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32404 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6775 POSTS0 COMMENTS
Nicole Veronica
11924 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11994 POSTS0 COMMENTS
Shaida Kate Naidoo
6903 POSTS0 COMMENTS
Ted Musemwa
7159 POSTS0 COMMENTS
Thapelo Manthata
6859 POSTS0 COMMENTS
Umr Jansen
6846 POSTS0 COMMENTS