Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Syntax:
3.2.
Parameters:
3.3.
Return Value: 
4.
Examples 
4.1.
Checking for a Number in an Array:
4.2.
JavaScript
4.3.
Searching for a Property Value in an Object:
4.4.
JavaScript
4.5.
Finding a Substring in a String:
4.6.
JavaScript
4.7.
Using fromIndex for Array Search:
4.8.
JavaScript
5.
Frequently Asked Questions 
5.1.
How does _.includes() differ from native JavaScript includes?
5.2.
Can _.includes() be used to find partial objects in an array?
5.3.
Is _.includes() case-sensitive when searching in strings?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.includes() Method

Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM

Introduction

Checking for the presence of a value within a collection is a common task in JavaScript. Lodash offers an elegant solution with its _.includes() method. This function checks if a given value is present in an array, object, or string, making it a versatile tool for various applications. 

Lodash _.includes() Method

This article will delve into the _.includes() method, illustrating its utility through examples and addressing frequently asked questions.

Why This Function is Used

The _.includes() function is used to determine whether a collection (array, object, or string) contains a specific value. It is especially useful in scenarios involving validation checks, feature toggling, or searching within data structures. By providing a straightforward way to check for the existence of an element, it enhances code readability and simplifies conditional logic.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Syntax, Parameter and Return Value

Syntax:

 _.includes(collection, value, [fromIndex=0])

Parameters:

  • collection (Array|Object|string): The collection to search.
     
  • value: The value to search for.
     
  • [fromIndex=0] (number): The index to start the search from.

Return Value: 

(boolean) - Returns true if value is found, else false.

Examples 

Checking for a Number in an Array:

  • JavaScript

JavaScript

var _ = require('lodash');

var numbers = [1, 2, 3, 4, 5];

var includesTwo = _.includes(numbers, 2);

console.log(includesTwo);

 Output: 

true


Demonstrates searching for a number in an array.

Searching for a Property Value in an Object:

  • JavaScript

JavaScript

var object = { 'a': 1, 'b': 2, 'c': 3 };

var includesThree = _.includes(object, 3);

console.log(includesThree);

 Output: 

true


Shows how to search for a value within an object's properties.

Finding a Substring in a String:

  • JavaScript

JavaScript

var string = "hello world";

var includesHello = _.includes(string, "hello");

console.log(includesHello);

 Output: 

true


An example of checking for a substring within a string.

Using fromIndex for Array Search:

  • JavaScript

JavaScript

var includesTwoAfterIndexTwo = _.includes(numbers, 2, 2);

console.log(includesTwoAfterIndexTwo);

Output:

 false


Demonstrates searching for an element in an array starting from a specific index.

Frequently Asked Questions 

How does _.includes() differ from native JavaScript includes?

Lodash's _.includes() works across different types of collections, including arrays, objects, and strings, unlike the native includes which is limited to arrays and strings.

Can _.includes() be used to find partial objects in an array?

_.includes() checks for primitive values and will not match partial objects; it compares object references for equality.

Is _.includes() case-sensitive when searching in strings?

Yes, when searching within strings, _.includes() is case-sensitive and will only find exact matches.

Conclusion

Lodash's _.includes() method is a versatile and efficient way to check for the presence of a value within various types of collections. It simplifies the process of searching and validating data, enhancing the functionality and readability of JavaScript code.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass