{"id":1474,"date":"2024-01-23T13:00:00","date_gmt":"2024-01-23T13:00:00","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=1474"},"modified":"2024-01-25T08:27:24","modified_gmt":"2024-01-25T08:27:24","slug":"trim-whitespace-javascript","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/trim-whitespace-javascript\/","title":{"rendered":"Trim Whitespace in JavaScript using trim() Method"},"content":{"rendered":"\n<p>When working with strings, it often requires removing leading and trailing whitespace. In this article, we will learn how to trim whitespace in a JavaScript string using the trim() method, with examples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to Trim Whitespace in JavaScript?<\/strong><\/h2>\n\n\n\n<p><strong>We commonly need to eliminate whitespaces in a string when working with user input.<\/strong> Users might unintentionally enter data with extra spaces when filling out a form or survey. Trimming whitespace will make the data more clean. It is also useful when we need to compare strings, we might want to ignore leading and trailing whitespaces. <\/p>\n\n\n\n<p><strong>JavaScript has a built-in trim() method to trim whitespaces from a string.<\/strong> This method belongs to the string class in JavaScript and allows us to remove leading and trailing whitespace characters from a given string.<\/p>\n\n\n\n<p>The syntax for the trim() method is:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background has-link-color wp-elements-41d2beb4b36721b2628f55a50a2dd9dd\" style=\"background-color:#fedcba\"><code>myString.trim()<\/code><\/pre>\n\n\n\n<p>It\u2019s important to note that the trim() method does not modify the original string. Instead, it returns a new string with the whitespace removed. This ensures that our data is intact and provides a clean, trimmed string to work with. Whitespace characters can include spaces, tabs, line feeds, carriage returns, and other similar characters.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Examples of using the trim() method<\/strong><\/h2>\n\n\n\n<p>Let&#8217;s explore different kinds of examples to demonstrate how the trim() function works.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1) String with Leading and Trailing Whitespace<\/strong><\/h3>\n\n\n\n<p>Suppose we have a string that has whitespace both at the starting as well as at the end. We can simply use the trim() method and get a clean string without whitespace. Let&#8217;s see 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;}\">\/\/ Define a string with leading and trailing whitespaces\nlet myString = &quot;   Hello Favtutor Readers   &quot;;\n\n\/\/ Use the trim() method to remove leading and trailing whitespaces\nlet trimmedString = myString.trim();\n\n\/\/ Display the result in the console\nconsole.log(trimmedString);<\/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 has-link-color wp-elements-e478eb6ff689a52628f5e60219e388d9\" style=\"background-color:#fedcba\"><code>\"Hello Favtutor Readers\"<\/code><\/pre>\n\n\n\n<p>Here we can see, that the trim() method successfully removes whitespace from both ends.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2) String Contains Only Whitespace<\/strong><\/h3>\n\n\n\n<p>In this case, the trim() method effectively removes all the whitespace from the string, leaving us with an empty string. It shows an input with all the whitespace is equivalent to an empty string. Here\u2019s the demonstration:<\/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;}\">\/\/ Define a string consisting of only whitespaces\nlet whitespaceString = &quot;       &quot;;\n\n\/\/ Use the trim() method to remove leading and trailing whitespaces\nlet trimmedWhitespaceString = whitespaceString.trim();\n\n\/\/ Display the result in the console\nconsole.log(trimmedWhitespaceString);<\/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 has-link-color wp-elements-e8d2b1ba89063d2630e91302041ce649\" style=\"background-color:#fedcba\"><code>\"\"<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3) String with No Whitespace<\/strong><\/h3>\n\n\n\n<p>When there is no whitespace to remove, the trim() method returns our input string unchanged. Sometimes we are not sure whether our string contains whitespace or not. Let&#8217;s see the demonstration:<\/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;}\">\/\/ Define a string without leading or trailing whitespaces\nlet noWhitespaceString = &quot;Favtutor&quot;;\n\n\/\/ Use the trim() method to remove any potential leading or trailing whitespaces (in this case, none)\nlet trimmedNoWhitespaceString = noWhitespaceString.trim();\n\n\/\/ Display the result in the console\nconsole.log(trimmedNoWhitespaceString); <\/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 has-link-color wp-elements-673a43f295ded2daed73ccd81855a3b3\" style=\"background-color:#fedcba\"><code>\"Favtutor\"<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Additional trim() Methods<\/strong><\/h3>\n\n\n\n<p>While the trim() method removes whitespace from both ends of a string, we have two more methods i.e. trimStart() and trimEnd(). These methods were introduced in <a href=\"https:\/\/262.ecma-international.org\/10.0\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">ECMAScript 2019 (ES10)<\/a>. They provide more specific control when we only need to trim spaces from one end of a string, by keeping the other part intact.<\/p>\n\n\n\n<p><strong>The trimStart() method is useful when we want to remove extra whitespace only from the start of the string. It keeps all the other spaces as it is.<\/strong><\/p>\n\n\n\n<p>There is also another method trimLeft(). These methods can be used interchangeably, as they both serve the same function. Let&#8217;s see 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;}\">\/\/ Define a string with leading and trailing whitespaces\nlet myString = &quot;       Favtutor  &quot;;\n\n\/\/ Use the trimStart() method to remove leading whitespaces\nlet trimmedStartString = myString.trimStart();\n\n\/\/ Display the result in the console\nconsole.log(trimmedStartString);<\/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 has-link-color wp-elements-299c46f231d9d9c98fb1e6180076c573\" style=\"background-color:#fedcba\"><code>\"Favtutor  \"<\/code><\/pre>\n\n\n\n<p>Here we can see only leading whitespaces are removed, while the ending whitespaces are unchanged.<\/p>\n\n\n\n<p><strong>The trimEnd() method removes all the whitespaces present at the end of a string and does not affect leading spaces. It is the counterpart to trimStart and focuses on ending whitespace characters.<\/strong><\/p>\n\n\n\n<p>It also has a similar method, trimRight(). They both keep leading whitespace untouched. Here\u2019s the code:<\/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;}\">\/\/ Define a string with leading and trailing whitespaces\nlet myString = &quot;  Hello Favtutor Readers!       &quot;;\n\n\/\/ Use the trimEnd() method to remove trailing whitespaces\nlet trimmedEndString = myString.trimEnd();\n\n\/\/ Display the result in the console\nconsole.log(trimmedEndString);<\/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 has-link-color wp-elements-142fbc02727e8e886d59a2b7899b8099\" style=\"background-color:#fedcba\"><code> \"  Hello Favtutor  Readers!\"<\/code><\/pre>\n\n\n\n<p>Also, learn how to <a href=\"https:\/\/favtutor.com\/articles\/remove-last-character-from-string-javascript\/\">remove the last character from a string in JavaScript<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In this article, we learned about the built-in method provided by JavaScript to trim whitespaces from a string. We also learned about trimStart() and trimEnd() which provide more targeted solutions, enabling the selective removal of leading or trailing whitespaces. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Understand how to trim whitespaces in JavaScript using the trim() method, along with its use cases and additional functions.<\/p>\n","protected":false},"author":13,"featured_media":1476,"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,38],"class_list":["post-1474","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-development","tag-javascript","tag-javascript-strings"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1474","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\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/comments?post=1474"}],"version-history":[{"count":4,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1474\/revisions"}],"predecessor-version":[{"id":1513,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1474\/revisions\/1513"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/1476"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=1474"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=1474"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=1474"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}