<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Sunil Kumar</title>
    <description>The latest articles on Forem by Sunil Kumar (@xerosource01).</description>
    <link>https://forem.com/xerosource01</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1024756%2F5f529fbb-f3f0-43e8-9b9c-340be587a9d5.jpg</url>
      <title>Forem: Sunil Kumar</title>
      <link>https://forem.com/xerosource01</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/xerosource01"/>
    <language>en</language>
    <item>
      <title>JavaScript String includes()</title>
      <dc:creator>Sunil Kumar</dc:creator>
      <pubDate>Sat, 11 Feb 2023 16:12:18 +0000</pubDate>
      <link>https://forem.com/xerosource01/javascript-string-includes-40k6</link>
      <guid>https://forem.com/xerosource01/javascript-string-includes-40k6</guid>
      <description>&lt;h2&gt;
  
  
  String includes() Method
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;String.prototype.includes()&lt;/strong&gt; method in JavaScript is used to check if a particular string is present within another string. It returns true if the specified string is found and false otherwise.&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic syntax of the string includes() Method
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;string.includes(searchString [, startIndex]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's an example of how to use the includes() method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const str = 'Hello, World!';

console.log(str.includes('Hello')); // true
console.log(str.includes('world')); // false (case-sensitive)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The includes() method has the following syntax:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;string.includes(searchString[, position])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;searchString:&lt;/strong&gt; The string to search for.&lt;br&gt;
&lt;strong&gt;position:&lt;/strong&gt; (optional) The position in the original string at which to begin the search. Default value is 0.&lt;/p&gt;

&lt;p&gt;You can also use the &lt;a href="https://xerosource.com/javascript-string-includes-method/"&gt;includes() method&lt;/a&gt; with the indexOf() method to check if a string is present within another string, but the includes() method is more readable and easier to use in comparison.&lt;/p&gt;

&lt;p&gt;It is worth noting that the includes() method is case-sensitive. To check for the presence of a string in a case-insensitive manner, you can convert both the source string and the search string to lowercase using the toLowerCase() method before using the includes() method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const str = 'Hello, World!';
const searchString = 'hello';

console.log(str.toLowerCase().includes(searchString.toLowerCase())); // true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another advantage of using the &lt;strong&gt;includes()&lt;/strong&gt; method over the indexOf() method is that it is supported in all modern browsers and can be used in both client-side and server-side JavaScript.&lt;/p&gt;

&lt;p&gt;You can also use the &lt;strong&gt;includes()&lt;/strong&gt; method to check if an array contains a certain element. For instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr = [1, 2, 3, 4, 5];
console.log(arr.includes(3)); // true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, the &lt;strong&gt;includes()&lt;/strong&gt; method will search for the given element in the array and return true if it is found, or false otherwise.&lt;/p&gt;

&lt;p&gt;Additionally, the &lt;strong&gt;includes()&lt;/strong&gt; method is also useful when checking for the presence of substrings within a string. For example, you can use it to check if a string ends with a particular suffix:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const str = 'Hello, World!';
const suffix = 'World!';

console.log(str.includes(suffix)); // true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also use the &lt;strong&gt;includes()&lt;/strong&gt; method in combination with other string methods, such as &lt;strong&gt;slice()&lt;/strong&gt; or &lt;strong&gt;substr()&lt;/strong&gt;, to extract substrings based on their presence within a string:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const str = 'Hello, World!';
const suffix = 'World!';

if (str.includes(suffix)) {
  const index = str.indexOf(suffix);
  const extracted = str.slice(index);
  console.log(extracted); // World!
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In conclusion, the &lt;strong&gt;includes()&lt;/strong&gt; method is a useful tool for checking if a string or array contains a specific substring or element. &lt;/p&gt;

&lt;p&gt;It is easy to use and provides a more readable alternative to other methods, such as indexOf().&lt;/p&gt;

&lt;h2&gt;
  
  
  Explanation of its search algorithm
&lt;/h2&gt;

&lt;p&gt;When determining whether a given substring is present in a target string, JavaScript's string &lt;strong&gt;includes()&lt;/strong&gt; method employs a linear search algorithm.&lt;/p&gt;

&lt;p&gt;If no &lt;strong&gt;startIndex&lt;/strong&gt; is specified, the method starts at the beginning of the string and searches for the &lt;strong&gt;searchString&lt;/strong&gt; from there.&lt;/p&gt;

&lt;p&gt;The method then repeatedly iterates through the string's characters, comparing each one to those in the searchString. &lt;/p&gt;

&lt;p&gt;The method returns true, indicating that the searchString is present in the string, if all the characters of the searchString are located in the target string. &lt;/p&gt;

&lt;p&gt;The method immediately returns false, indicating that the searchString is not present in the string, if any character of the searchString is not found in the target string.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;includes()&lt;/strong&gt; method's search algorithm is case-sensitive, which means that when comparing characters from the searchString and the target string, it takes their case into account. &lt;/p&gt;

&lt;p&gt;The method will return false if the searchString is case-sensitive and the target string contains a case-insensitive version of the searchString.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
