Friday, September 19, 2025
HomeLanguagesJavascriptLodash _.hasIn() Method

Lodash _.hasIn() Method

The _.hasIn() method is used to check whether the path is a direct or inherited property of object or not. It returns true if path exists, else it returns false.

Syntax:

_.hasIn(object, path)

Parameters: This method accepts two parameters as mentioned above and described below:

  • object: This parameter holds the object to query.
  • path: This parameter holds the path to check. The path will be array or string.

Return Value: This method returns true if path exists, else false

Example 1:

Javascript




// Requiring the lodash library  
const _ = require("lodash");  
  
// Given object
var object =  _.create({ 'a': _.create({ 'b': 2 }) });
  
// Use of _.hasIn method 
console.log(_.hasIn(object, 'a')); 
console.log(_.hasIn(object, ['a'])); 
console.log(_.hasIn(object, ['b'])); 


Output:

true
true
false

Example 2:  

Javascript




// Requiring the lodash library  
const _ = require("lodash");  
  
// Given object
var object =  _.create({ 'a': _.create({ 'b': 2 }) });
  
// Use of _.hasIn method 
console.log(_.hasIn(object, 'a.b')); 
console.log(_.hasIn(object, ['a','b'])); 
console.log(_.hasIn(object, ['a','b','c'])); 


Output:

true
true
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
32301 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6666 POSTS0 COMMENTS
Nicole Veronica
11840 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11898 POSTS0 COMMENTS
Shaida Kate Naidoo
6781 POSTS0 COMMENTS
Ted Musemwa
7056 POSTS0 COMMENTS
Thapelo Manthata
6739 POSTS0 COMMENTS
Umr Jansen
6745 POSTS0 COMMENTS