{"id":1132,"date":"2023-12-19T21:00:00","date_gmt":"2023-12-19T21:00:00","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=1132"},"modified":"2023-12-23T12:13:39","modified_gmt":"2023-12-23T12:13:39","slug":"javascript-string-startswith","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/javascript-string-startswith\/","title":{"rendered":"JavaScript String startsWith() Method (with Examples)"},"content":{"rendered":"\n<p>Strings represent a sequence of multiple characters and it is crucial to know how to manipulate and play with strings in any programming language. One of the ways to manipulate strings is by using startsWith in JavaScript.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong><strong>What is the startsWith() Function in JavaScript?<\/strong><\/strong><\/h2>\n\n\n\n<p>Before moving forward, let&#8217;s revise our understanding of Strings. Strings in JavaScript represent a sequence of multiple characters. We can declare a string in JavaScript by making a string literal using double quotes. The syntax for the same is given below:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring a string literal\nlet a = &quot;FavTutor&quot;\n\n\/\/printing the string\nconsole.log(a);<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>FavTutor<\/code><\/pre>\n\n\n\n<p>So, where does startsWith() fit in?<\/p>\n\n\n\n<p><strong>The startsWith() is a built-in JavaScript method to determine or check whether a string starts with a specified set of characters. The method returns a bool value indicating the presence of the characters at the beginning of the string.<\/strong><\/p>\n\n\n\n<p>The syntax of the method is given below:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>stringName.startsWith(stringCheck, position);<\/code><\/pre>\n\n\n\n<p>where,<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>stringName: <\/strong>The original string on which we have to perform the check<\/li>\n\n\n\n<li><strong>stringCheck<\/strong>: The substring to be checked for at the beginning of the original string.<\/li>\n\n\n\n<li><strong>position(optional parameter): <\/strong>The position within the original string at which to begin the search. By default, position would be index 0, i.e. beginning of the string.<\/li>\n<\/ul>\n\n\n\n<p>It returns a boolean value: true if the string starts with the specified characters, and false otherwise. The method first compares the characters of the given string with the given sequence of characters. If the characters match from the beginning of the string, the function returns true. However, if doesn&#8217;t match it returns false. <\/p>\n\n\n\n<p>Let us see the use of startsWith with an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring the original string\nlet ogString=&quot;FavTutor&quot;;\n\n\/\/declaring the check string\nlet subString=&quot;Fav&quot;;\n\n\/\/checking if substring is part of the original string\nlet checkBool=ogString.startsWith(subString);\n\n\/\/printing the return value\nconsole.log(checkBool);<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>True<\/code><\/pre>\n\n\n\n<p>In this example, we have taken 2 strings, one original string(ogString), and a check string(subString). We check the presence of the subString in the ogString. We have used no position parameter in the function, so by default, we check the presence of the subString at the starting index, i.e. 0 of the ogString.\u00a0<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Positional Parameter<\/strong><\/h3>\n\n\n\n<p>Let us see another example, of using the positional parameter with the startsWith function.&nbsp;<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring the original string\nlet ogString=&quot;FavTutor&quot;;\n\n\/\/declaring the check string\nlet subString=&quot;Fav&quot;;\n\n\/\/checking if substring is part of the original string\nlet checkBool=ogString.startsWith(subString,2);\n\n\/\/printing the return value\nconsole.log(checkBool);<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>false<\/code><\/pre>\n\n\n\n<p>In this example, we have provided the position parameter as 2. This means that we will verify the presence of the characters of the subString starting from the second index of the original string(ogString) that is from the letter \u2018v\u2019. Hence we have got the result as false. This is the desired result.<\/p>\n\n\n\n<p>Let us see a similar example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring the original string\nlet ogString=&quot;FavTutor&quot;;\n\n\/\/declaring the check string\nlet subString=&quot;Tutor&quot;;\n\n\/\/checking if substring is part of original string\nlet checkBool=ogString.startsWith(subString,3);\n\n\/\/printing the return value\nconsole.log(checkBool);<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>true<\/code><\/pre>\n\n\n\n<p>Now, in this example, we are using the positional parameter as 3. We are checking the subString \u201cTutor\u201d to be present in the string \u201cFavTutor &#8221; at the 3rd index. As the indices start from 0 the string \u201cTutor\u201d occurs at the 3rd index in the ogString. Hence, we have got the desired result.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Case Sensitivity<\/strong><\/h3>\n\n\n\n<p>It is also important to note the case sensitivity of the startsWith function that the uppercase and lowercase letters are treated differently. Let us see an example of the same:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring the original string\nlet ogString=&quot;FavTutor&quot;;\n\n\/\/declaring the check string\nlet subString=&quot;fav&quot;;\n\n\/\/checking if substring is part of original string\nlet checkBool=ogString.startsWith(subString);\n\n\/\/printing the return value\nconsole.log(checkBool);<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>false<\/code><\/pre>\n\n\n\n<p>We are checking the presence of \u201cfav\u201d in the string \u201cFavTutor\u201d. Although the starting three characters are the same in the string \u201cFavTutor\u201d, the letter \u2018f\u2019 is present in uppercase and hence doesn\u2019t match with the letter \u2018f\u2019 present in lowercase in the string \u201cfav\u201d. Hence the function returns false. We have got the desired result.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In this article, we have studied about JavaScript method \u201cstartsWith\u201d which helps in finding whether a specified string is present in another string at a specified position. Let&#8217;s now learn how to <a href=\"https:\/\/favtutor.com\/articles\/concatenate-strings-javascript\/\" data-type=\"link\" data-id=\"https:\/\/favtutor.com\/articles\/concatenate-strings-javascript\/\">concatenate strings in JavaScript<\/a> to fully understand it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Understand everything about the startsWith() method in JavaScript to check if a string starts with a certain string.<\/p>\n","protected":false},"author":9,"featured_media":1134,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jnews-multi-image_gallery":[],"jnews_single_post":null,"jnews_primary_category":{"id":"","hide":""},"footnotes":""},"categories":[9],"tags":[11],"class_list":["post-1132","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-development","tag-javascript"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/comments?post=1132"}],"version-history":[{"count":4,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1132\/revisions"}],"predecessor-version":[{"id":1160,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1132\/revisions\/1160"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/1134"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=1132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=1132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=1132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}