JavaScript Array includes()
In this post, we will discuss includes() method available in JavaScript Array.
includes()
The includes() method is used to check whether the given element exists in the array or not. If the element, exists in the array, true is returned, otherwise false is returned.
Syntax:
array1.includes(element,start_pos)
It will take two parameters.
- First parameter is the element to be searched
- Second parameter is the integer that will specify the start position in which the element to be searched. This parameter is optional. By default, search starts from start position 0.
Example 1:-
Let's create an array that hold 10 subjects and search for jsp,html and hadoop.
Copied
<html>
<body>
<script>
// Create an array that hold 10 strings
const subjects1 =["php","html","node.js","java","jsp","jsp","php","php","java","php"];
document.writeln("Subjects: <br>");
document.writeln(subjects1);
document.writeln("<br>");
document.writeln("<br>");
// Search for the element-jsp.
document.writeln("Search for jsp: ");
document.writeln(subjects1.includes("jsp"));
document.writeln("<br>");
// Search for the element-html.
document.writeln("Search for html: ");
document.writeln(subjects1.includes("html"));
document.writeln("<br>");
// Search for the element-hadoop.
document.writeln("Search for hadoop: ");
document.writeln(subjects1.includes("hadoop"));
document.writeln("<br>");
</script>
</body>
</html>
Output:
Subjects:
php,html,node.js,java,jsp,jsp,php,php,java,php
Search for jsp: true
Search for html: true
Search for hadoop: false
- "jsp" exists in the array. so true is returned.
- "html" exists in the array. so true is returned.
- "hadoop" does not exist in the array. so false is returned.
Example 2:-
Let's create an array that hold 10 subjects and search for jsp and html from particular positions.
Copied
<html>
<body>
<script>
// Create an array that hold 10 strings
const subjects1 =["php","html","node.js","java","jsp","jsp","php","php","java","php"];
document.writeln("Subjects: <br>");
document.writeln(subjects1);
document.writeln("<br>");
document.writeln("<br>");
// Search for the element-jsp from 7th position.
document.writeln("Search for jsp from 7th position: ");
document.writeln(subjects1.includes("jsp",6));
document.writeln("<br>");
// Search for the element-jsp from 6th position.
document.writeln("Search for jsp from 6th position: ");
document.writeln(subjects1.includes("jsp",5));
document.writeln("<br>");
// Search for the element-html from 7th position..
document.writeln("Search for html from 7th position: ");
document.writeln(subjects1.includes("html",6));
document.writeln("<br>");
</script>
</body>
</html>
Output:
Subjects:
php,html,node.js,java,jsp,jsp,php,php,java,php
Search for jsp from 7th position: false
Search for jsp from 6th position: true
Search for html from 7th position: false
- "jsp" does not exists in the array from 7th position (Index-6). so false is returned.
- "jsp" exists in the array from 6th position (Index-5). so true is returned.
- "html" does not exist in the array from 7th position (Index-6). so false is returned.
Example 3:-
Let's create an array that hold 10 subjects and search for php and java from particular positions.
Copied
<html>
<body>
<script>
// Create an array that hold 10 strings
const subjects1 =["php","html","node.js","java","jsp","jsp","php","php","java","php"];
document.writeln("Subjects: <br>");
document.writeln(subjects1);
document.writeln("<br>");
document.writeln("<br>");
// Search for the element-java from 7th position.
document.writeln("Search for java from 7th position: ");
document.writeln(subjects1.includes("java",6));
document.writeln("<br>");
// Search for the element-php from 6th position.
document.writeln("Search for php from 6th position: ");
document.writeln(subjects1.includes("php",5));
document.writeln("<br>");
// Search for the element-php from 1st position..
document.writeln("Search for php from 1st position: ");
document.writeln(subjects1.includes("php",0));
document.writeln("<br>");
</script>
</body>
</html>
Output:
Subjects:
php,html,node.js,java,jsp,jsp,php,php,java,php
Search for java from 7th position: true
Search for php from 6th position: true
Search for php from 1st position: true
- "java" exists in the array from 7th position (Index-6). so true is returned
- "php" exists in the array from 6th position (Index-5). so true is returned.
- "php" exists in the array from 1st position (Index-0). so true is returned.