As a programming language, JavaScript offers a variety of built-in functions that help developers achieve different tasks efficiently. One such function is the “includes()” function.
Introduction
JavaScript is one of the most popular programming languages for building web applications. It provides developers with a variety of functions that simplify the process of writing code. One of these functions is the “includes()” function.
If you have worked with JavaScript for some time, you have probably come across this function. However, if you are new to the language, you might not be familiar with it. In this article, we will explore the “includes()” function in detail and demonstrate how it can be used to make your code more efficient.
What is the includes method in javascript in JavaScript?
It is a built-in function in JavaScript that allows developers to check whether a given value exists in an array or a string. The function returns a boolean value indicating whether the value is present in the array or string.
Syntax of the includes method in javascript
The syntax of the “includes()” function is as follows:
array.includes(searchElement[, fromIndex])
string.includes(searchString[, position])
Parameters of the includes method in javascript
This function takes two parameters:
- searchElement – This is the element that the function searches for in the array.
- fromIndex (optional) – This is the index from which the search begins. If not specified, the search begins at index 0.
For strings, the parameters are slightly different:
- searchString – This is the string that the function searches for.
- position (optional) – This is the index from which the search begins. If not specified, the search begins at index 0.
Return value of the includes method in javascript
This returns a boolean value indicating whether the search element or string is present in the array or string. If the element or string is found, the function returns true
. Otherwise, it returns false
.
Example usage of the includes method in javascript
Let’s look at an example of how function can be used in JavaScript. Suppose we have an array of numbers, and we want to check if a specific number is present in the array:
const numbers = [1, 2, 3, 4, 5];
const searchElement = 3;
console.log(numbers.includes(searchElement)); // true
In this example, we define an array of numbers and a search element. We then use the “includes()” function to check whether the search element is present in the array. The function returns true
because the number 3 is present in the array.
This function can also be used with strings. Suppose we have a string, and we want to check if a specific substring is present in the string:
const string = "Hello World";
const searchString = "World";
console.log(string.includes(searchString)); // true
In this example, we define a string and a search string. We then use this function to check whether the search string is present in the string. The function returns true
because the substring “World” is present in the string.
The difference between the “includes()” and “indexOf()” functions
The “includes()” function and the “indexOf()” function are similar in that they can be used to check whether an element or substring is present in an array or string. However, there is a key difference between the two functions.
This function returns a boolean value indicating whether the search element or string is present in the array or string. On the other hand, the “indexOf()” function returns the index of the first occurrence of the search element or string. If the element or string is not found, the function returns -1.
Here’s an example of how the “indexOf()” function can be used to achieve the same result as the “includes()” function:
const numbers = [1, 2, 3, 4, 5];
const searchElement = 3;
console.log(numbers.indexOf(searchElement) !== -1); // true
In this example, we use the “indexOf()” function to check whether the search element is present in the array. If the function returns a value other than -1, we know that the element is present in the array.
Using the “includes()” function to search for multiple occurrences
This function can also be used to search for multiple occurrences of a value in an array. For example, suppose we have an array of numbers, and we want to check if the number 3 is present multiple times in the array:
const numbers = [1, 2, 3, 3, 4, 5];
console.log(numbers.includes(3)); // true
console.log(numbers.includes(3, 3)); // true
In this example, we use the “includes()” function to check whether the number 3 is present in the array. We first use the function without the “fromIndex” parameter, which returns true
because the number 3 is present in the array. We then use the function with the “fromIndex” parameter set to 3, which starts the search from the index 3. Since there are two occurrences of the number 3 in the array, the function returns true
.
Limitations of the “includes()” function
Although the “includes()” function is a powerful tool for searching arrays and strings, there are some limitations to its usage. For example, the function cannot be used to search for objects in an array. If you need to search for objects in an array, you will need to use a different method.
Additionally, the “includes()” function does not work in all browsers. If you need to use the function in an older browser, you may need to use a polyfill or a different method altogether.
Conclusion
The “includes()” function is a useful tool for searching arrays and strings in JavaScript. By understanding how the function works and its limitations, you can use it to write more efficient and effective code.
Follow Us on
https://www.linkedin.com/company/scribblers-den/
https://www.facebook.com/scribblersden.blogs
Read More
https://scribblersden.com/can-we-make-an-iron-man-suit/
Thank You