contains() in JavaScript- How and when to use it?
Hey readers, in this article we will be discussing how to use contains() method in JavaScript in a step-wise, efficient manner. If you are a beginner and want to learn JavaScript then do check out the course on Codedamn.
Introduction
JavaScript is one of the most used languages when it comes to building web applications as it allows developers to wrap HTML and CSS code in it to make web apps interactive. It allows a web application to be interactive. It is also used for making animations on websites and has a large community on GitHub. JavaScript has tons of libraries, one of which is React which we will be covering in this article later.
Use cases:
- Building web server and its interactive functions
- Animations and graphics, adding special effects to web components
- Validating forms and exception errors
- Adding behavior and functionalities to web pages
Since JavaScript is a scripting language, it works without any libraries. Developers are free to write JavaScript language code anywhere and can still run it in the browser. There is no structure in JavaScript, the browser executes each line of the code from top to down.
contains() Method
The contains() method is used to check whether or not a collection contains a specific item. If the item is present, it returns true; otherwise, it returns false. The function is applied to the collection once the JavaScript array is turned into a collection.
Syntax:
collect(array).contains(item)
Parameters: This method takes a single parameter, which is turned into a collection, and then the contains() function takes an item to search.
Return Value: A boolean value is returned.
The contains() method in Collect.js is demonstrated in the following examples:
Example 1: The collect.js library is imported into the file with collect = require(‘collect.js’)
const collect = require('collect.js');
let arr = [1, 2, 3];
// Convert array into collection
const collection = collect(arr);
// item to searched
let item = 3;
let newObject = collection.contains(item);
console.log("Result : ", newObject);
Example 2:
const collect = require('collect.js');
let arr = [1, 2, 3]
// Convert array into collection
const collection = collect(arr);
// concate the array
let concatarr = collection.concat(['a', 'b', 'c']);
// concate the object
concatarr = concatarr.concat({ first : "Codedamn", second : "Collect.js"});
// item to searched
let item = "Codedamn";
let newObject = collection.contains(item);
console.log("Result : ", newObject);
Working of contains() method in JavaScript
The Node interface’s includes() method provides a boolean result that indicates if a node is a descendant of a certain node, such as the node itself, one of its direct children (childNodes), one of the children’s direct children, and so on.
Parameters
otherNode
The Node to test with.
The value of the return
If otherNode is contained in the node, this value is true; otherwise, it is false.
contains() always returns false if the otherNode parameter is null.
Conclusion
This was about contains method in JavaSript using HTML, if you want to learn more about javascript, do check out the article and course on Codedamn of javascript along with the course. Hope you liked this, if you have any queries or suggestions do let us know in the comments.
If you are interested in learning JavaScript, do check out courses on codedamn with an in-built environment playground to learn and practice code. Join the community of codedamn and do check out other articles related to programming and development on codedamn and subscribe to our newsletter to never miss out on our new programs and updates.
If you have any queries or feedback do let us know in the comment section.
Sharing is caring
Did you like what Agam singh, Aman Ahmed Siddiqui, Aman Chopra, Aman, Amol Shelke, Anas Khan, Anirudh Panda, Ankur Balwada, Anshul Soni, Arif Shaikh, wrote? Thank them for their work by sharing it on social media.
No comments so far
Curious about this topic? Continue your journey with these coding courses: