How to trim a string at beginning or ending in JavaScript ?

13 Dec, 2021
This article demonstrates how to trim a string at the beginning, end, and also from both sides. For various sorts of string trimming, JavaScript provides three functions. TrimLeft(), used to remove characters from the beginning of a string. TrimRight(), used to remove characters from the string’s end. Trim(), used to remove characters from both ends. JavaScript’s native functions, like those of many other languages, solely remove whitespace characters. We will discuss all these functions in detail, & understand them through the examples.
Trimming a string at the beginning: In this case, we trim the string at the beginning using the trimLeft() function. 
JavaScript trimLeft() Function: This method is used to eliminate white space at the beginning of a string. The string’s value is not changed in any way, if any white space is present after the string, it’s not modified.
 
Syntax:
string.trimLeft();
Example 1: In this example, a variable var is declared with the string ” geeksforgeeks”. Notice the given string that has whitespace at the left end. The trimLeft() function will remove the whitespace at the beginning.
HTML
<!DOCTYPE html><html>  <head>    <title>Javascript trimLeft() Function</title></head>  <body>    <script>    var word = " geeksforgeeks";    console.log("initial string:" + "'" + word + "'");          // Trimming the string at the Beginning     var new_word = word.trimLeft();    console.log("modified string:" + "'" + new_word + "'");    </script></body>  </html>
Output:
initial string:' geeksforgeeks'
modified string:'geeksforgeeks'
Example 2: In this example, a variable var is declared with the string ” geeksforgeeks “. Notice the given string that has whitespace at both ends. trimLeft() will only remove the whitespace at the beginning and leaves the whitespace at the end unchanged.
HTML
<!DOCTYPE html><html>  <head>    <title>Javascript trimLeft() Function</title></head>  <body>    <script>    var word = " geeksforgeeks ";    console.log("initial string:" + "'" + word + "'");          // Trimming the string at the start    var new_word = word.trimLeft();    console.log("modified string:" + "'" + new_word + "'");    </script></body>  </html>
Output:
initial string:' geeksforgeeks '
modified string:'geeksforgeeks '
Trimming the string at the end: In this case, we trim the string at the end using the trimRight() function.
JavaScript trimRight() Function: This method is used to eliminate white-space from the end of a string. The string’s value is not changed in any way, if any white space is present before the string, it’s not modified.
Syntax:
string.trimRight();
Example 1: In this example, a variable var is declared and string “geeksforgeeks ” is given to it. Notice the given string which has whitespace at the right end, so trimRight() removes the whitespace at the end.
HTML
<!DOCTYPE html><html>  <head>    <title>Javascript trimRight() Function</title></head>  <body>    <script>    var word = "geeksforgeeks ";    console.log("initial string:" + "'" + word + "'");          // Trimming the string at the right end    var new_word = word.trimRight();    console.log("modified string:" + "'" + new_word + "'");    </script></body>  </html>
Output:
initial string:'geeksforgeeks '
modified string:'geeksforgeeks'
Example 2: In this example, a variable var is declared and string ” geeksforgeeks ” is given to it. Notice the given string that has whitespace at both ends. The trimRight() function removes the whitespace at the end and not at the beginning.
HTML
<!DOCTYPE html><html>  <head>    <title>Javascript trimRight() Function</title></head>  <body>    <script>    var word = " geeksforgeeks ";    console.log("initial string:" + "'" + word + "'");          // Trimming the string at the right end    var new_word = word.trimRight();    console.log("modified string:" + "'" + new_word + "'");    </script></body>  </html>
Output:
initial string:' geeksforgeeks '
modified string:' geeksforgeeks'
Trimming the string from both the ends: In this case, we trim the string at both ends using the trim() function.
JavaScript trim() Function: Trim() eliminates whitespace from both ends of a string and produces a new string with no changes to the original. All whitespace characters and all line terminator characters are considered whitespace in this context.
Syntax:
string.trim();
Example: In this example, a variable var is declared and string ” geeksforgeeks ” is given to it. Notice the given string that has whitespace at both ends. The trim() removes the whitespace at both ends. 
HTML
<!DOCTYPE html><html>  <head>    <title>Javascript trim() Function</title></head>  <body>    <script>    var word = " geeksforgeeks ";    console.log("initial string:" + "'" + word + "'");          // Trimming the string at both ends    var new_word = word.trim();    console.log("modified string:" + "'" + new_word + "'");    </script></body>  </html>
Output:
initial string:' geeksforgeeks '
modified string:'geeksforgeeks'
Supported Browsers:
Google Chrome 4.0
Firefox 3.5
Internet Explorer 10.0
Microsoft Edge 12.0
Opera 10.5
Safari 5.0
JavaScript-Methods
JavaScript-Questions
Picked
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Remove elements from a JavaScript Array
Roadmap to Learn JavaScript For Beginners
Difference Between PUT and PATCH Request
JavaScript | Promises
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?