{"id":127,"date":"2020-08-02T15:09:37","date_gmt":"2020-08-02T15:09:37","guid":{"rendered":"https:\/\/datatype.co.in\/blog\/?p=127"},"modified":"2020-08-20T09:22:12","modified_gmt":"2020-08-20T09:22:12","slug":"how-to-check-if-a-string-contains-a-substring-in-javascript","status":"publish","type":"post","link":"https:\/\/datatype.co.in\/blog\/how-to-check-if-a-string-contains-a-substring-in-javascript\/","title":{"rendered":"How to Check If a String Contains a Substring in JavaScript"},"content":{"rendered":"\n<p>In this article I will discuss how you can check if a string contains particular substring.<\/p>\n\n\n\n<p><strong>1.<\/strong> Prior JavaScript ES6, the conventional way to check if a string contains a substring was to use the string method <strong>indexOf <\/strong>that return -1 if the string does not contain the substring. If the substring is found, it returns the index of the character that starts the string.  This method is supported by all browsers including Internet Explorer (IE).<\/p>\n\n\n\n<pre class=\"wp-block-code prettyprint\"><code>var message= \"Have a wonderful day.\";\nmessage.indexOf('wonderful') !== -1 \/\/ true\nmessage.indexOf('wonderful') \/\/ 7\n\n\/\/ Example: Check if message contains the string 'wonderful'\nif(message.indexOf('wonderful') !== -1){\n    \/\/ message contains the string 'wonderful'\n}<\/code><\/pre>\n\n\n\n<p><strong>2.<\/strong> ES6\/ES2015 has introduced a string method <strong>includes<\/strong> to check if a string contains substring.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code prettyprint\"><code>var message= \"Have a wonderful day.\";\nmessage.includes('wonderful') \/\/ true\n\n\/\/ Example: With ES6\nif(message.includes('wonderful')){\n    \/\/ message contains the string 'wonderful'\n}<\/code><\/pre>\n\n\n\n<p>The <strong>includes<\/strong> method is supported by all modern browsers except IE. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article I will discuss how you can check if a string contains particular substring. 1. Prior JavaScript ES6, the conventional way to check if a string contains a&nbsp;[ &hellip; ]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[91,92,90,89,88],"class_list":["post-127","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-js-check-if-string-contains-substring","tag-js-check-if-substring-exists-in-string","tag-js-includes","tag-js-indexof-example","tag-js-substring-in-string","list-style-post"],"_links":{"self":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/127","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/comments?post=127"}],"version-history":[{"count":18,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/127\/revisions"}],"predecessor-version":[{"id":354,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/127\/revisions\/354"}],"wp:attachment":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/media?parent=127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/categories?post=127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/tags?post=127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}