{"id":1677,"date":"2022-07-11T20:19:54","date_gmt":"2022-07-11T20:19:54","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=1677"},"modified":"2022-07-11T20:19:58","modified_gmt":"2022-07-11T20:19:58","slug":"javascript-string-indexof-method","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/javascript-string-indexof-method\/","title":{"rendered":"Javascript String Indexof() Method"},"content":{"rendered":"<p style=\"text-align: center;\"><strong>Javascript String Indexof() Method<\/strong><\/p>\n<p>As the name says, this method returns the index of the first occurrence of some specified string in some string. It&#8217;s like we are finding the first occurrence of some string inside a string.<\/p>\n<p>Remember that we have to give the string, whose first occurrence are we searching for. This is like we call the indexOf method, and just say that \u2013 hey indexOf, take this string, and return the first occurrence index of the string in the given string. So, simply the index of the first occurrence of some string will be returned.<\/p>\n<p>Remember that a character is also a single-length string here.<br \/>\nAlways, the string index starts from zero, so the first character in the string has an index of 0, and so on.<\/p>\n<p>If the string is not found, then this method returns -1.<\/p>\n<p>You can refer to the below code to understand the indexOf method. To call the indexOf method, we are writing the string variable name, followed by the dot, and then the method name(this is going to be followed further) &#8211;<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1678 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/06\/Finding-the-first-occurrence-index-of-some-string-in-the-stringindexOf-method.jpg\" alt=\"\" width=\"641\" height=\"93\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/06\/Finding-the-first-occurrence-index-of-some-string-in-the-stringindexOf-method.jpg 641w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/06\/Finding-the-first-occurrence-index-of-some-string-in-the-stringindexOf-method-300x44.jpg 300w\" sizes=\"auto, (max-width: 641px) 100vw, 641px\" \/>As you can see, we have used the indexOf method. The indexOf method returns the first occurrence index of the given string in the string. You can also store it somewhere if you need it further, but for demonstration purposes, we have directly printed it. Have a look at the output \u2192<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1679 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/06\/Finding-the-first-occurrence-index-of-some-string-in-the-stringindexOf-method-1.jpg\" alt=\"\" width=\"914\" height=\"93\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/06\/Finding-the-first-occurrence-index-of-some-string-in-the-stringindexOf-method-1.jpg 914w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/06\/Finding-the-first-occurrence-index-of-some-string-in-the-stringindexOf-method-1-300x31.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/06\/Finding-the-first-occurrence-index-of-some-string-in-the-stringindexOf-method-1-768x78.jpg 768w\" sizes=\"auto, (max-width: 914px) 100vw, 914px\" \/><\/p>\n<p>Also, we know that if the string has never occurred in the given string, then the indexOf method returns the value -1.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1680 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/06\/Finding-the-first-occurrence-index-of-some-string-in-the-stringindexOf-method-2.jpg\" alt=\"\" width=\"702\" height=\"97\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/06\/Finding-the-first-occurrence-index-of-some-string-in-the-stringindexOf-method-2.jpg 702w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/06\/Finding-the-first-occurrence-index-of-some-string-in-the-stringindexOf-method-2-300x41.jpg 300w\" sizes=\"auto, (max-width: 702px) 100vw, 702px\" \/><\/p>\n<p>As you can see, in the indexOf method, we can pass the string &#8216;funny&#8217; as an argument, but we know that the argument string is not present there in the given string. So, we get an output of -1. Have a look at the output &#8211;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1681 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/06\/Finding-the-first-occurrence-index-of-some-string-in-the-stringindexOf-method-3.jpg\" alt=\"\" width=\"908\" height=\"93\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/06\/Finding-the-first-occurrence-index-of-some-string-in-the-stringindexOf-method-3.jpg 908w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/06\/Finding-the-first-occurrence-index-of-some-string-in-the-stringindexOf-method-3-300x31.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/06\/Finding-the-first-occurrence-index-of-some-string-in-the-stringindexOf-method-3-768x79.jpg 768w\" sizes=\"auto, (max-width: 908px) 100vw, 908px\" \/><\/p>\n<p>As you can see, we got the output as -1, which was totally expected, since the string has never occurred here in the given string. We can make use of this method, as and when required.<br \/>\nYou just need to understand that this method returns the first occurrence index of the given string, in the string, and if the string has never occurred in the given string, then -1 is returned.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Javascript String Indexof() Method As the name says, this method returns the index of the first occurrence of some specified string in some string. It&#8217;s like we are finding the first occurrence of some string inside a string. Remember that we have to give the string, whose first occurrence are we searching for. This is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[14,15],"class_list":{"0":"post-1677","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-javascript-tutorial","7":"tag-javascript","8":"tag-javascript-tutorial"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/1677","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=1677"}],"version-history":[{"count":2,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/1677\/revisions"}],"predecessor-version":[{"id":2219,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/1677\/revisions\/2219"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=1677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=1677"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=1677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}