{"id":4901,"date":"2024-07-16T18:15:42","date_gmt":"2024-07-16T12:45:42","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=4901"},"modified":"2024-07-16T18:15:43","modified_gmt":"2024-07-16T12:45:43","slug":"what-is-the-difference-between-substring-and-slice-method-in-javascript","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/what-is-the-difference-between-substring-and-slice-method-in-javascript\/","title":{"rendered":"what is the difference between substring and slice method in javascript?"},"content":{"rendered":"<p>In JavaScript, we have a &#8220;string&#8221; dataType and it gives us some inbuilt functions now it gives us two functions one is the <strong>substring() <\/strong>and the second is the <strong>slice()<\/strong>.<\/p>\n<p><\/p>\n<p>now the <strong>substring<\/strong> method is used to get part of a string. Think of it as a way to cut out a piece of the string. You tell the substring() method where to start and where to stop cutting the string.<\/p>\n<p><\/p>\n<p>for example:<\/p>\n<pre class=\"ql-syntax\">let str = \"hello world!\"\nconsole.log(str.substring(0,2))\n\/\/output: \"he\"\n<\/pre>\n<p><\/p>\n<p>now the slice method also works in the same way it takes the starting index and end index and returns the character between the given indexes<\/p>\n<p><\/p>\n<p>for example:<\/p>\n<pre class=\"ql-syntax\">let str = \"hello world!\"\nconsole.log(str.slice(0,2))\n\/\/output: \"he\"\n<\/pre>\n<p><\/p>\n<p>the output of the slice and substring method is the same so what is the main difference between them?<\/p>\n<p>the main difference between the substring and slice methods is :<\/p>\n<ol>\n<li>Handling Negative Indices:<\/li>\n<\/ol>\n<p>substring(): If you use negative values, they are treated as 0.<\/p>\n<p>slice(): Negative values count from the end of the string.<\/p>\n<pre class=\"ql-syntax\">let str = \"Hello, World!\";\n console.log(str.substring(0,-1)) \n\/\/ Output: \"\" \nconsole.log(str.substring(-1,3)) \n\/\/ Output: \"Hel\" \nconsole.log(str.substring(1,-3))\n\/\/ Output: \"H\"\nconsole.log(str.substring(0,-1)) \/\/ Output: \"Hello, World\"\n<\/pre>\n<p>2. Order of Parameters:<\/p>\n<p>substring(): If the start index is greater than the end index, the substring will swap the values.<\/p>\n<p>slice(): If the start index exceeds the end index, the slice will return an empty string.<\/p>\n<pre class=\"ql-syntax\">let str = \"Hello, World!\";\nconsole.log(str.substring(4,2)) \n\/\/ Output: \"ll\" \nconsole.log(str.slice(4,2)) \n\/\/ Output: \"\"\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In JavaScript, we have a &#8220;string&#8221; dataType and it gives us some inbuilt functions now it gives us two functions one is the substring() and the second is the slice(). now the substring method is used to get part of a string. Think of it as a way to cut out a piece of the<\/p>\n","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[334,172],"tags":[335,330],"class_list":["post-4901","post","type-post","status-publish","format-standard","category-best-practices","category-javascript","tag-best-practices","tag-javascript"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/4901","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=4901"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/4901\/revisions"}],"predecessor-version":[{"id":4902,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/4901\/revisions\/4902"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=4901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=4901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=4901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}