JavaScript | trimStart() and trimLeft() Method - GeeksforGeeks

30 Dec, 2019
The trimStart() method in JavaScript is used to remove whitespace from the beginning of a string. The value of the string is not modified in any manner, including any whitespace present after the string.
Syntax:
string.trimStart()
Return Value: It returns the final string that is stripped out of all the white-space in the beginning.
Example: This example implements the trimStart() method.
<!DOCTYPE html><html>  <head>    <title>        JavaScript trimStart() method    </title></head>  <body>    <h1 style="color: green">        GeeksforGeeks    </h1>          <b>        JavaScript | trimStart() method    </b>          <pre>Output for " GeeksforGeeks     " : <span                class="output"></span>    </pre>          <pre>Output for "Hello There! " : <span                class="output_2"></span>    </pre>          <button onclick="trimString()">        Trim Start    </button>          <script type="text/javascript">        function trimString() {            str1 = " GeeksforGeeks     ";            str2 = "Hello There! ";                  trimmed_out = str1.trimStart();            trimmed_out2 = str2.trimStart();                  document.querySelector('.output')                .textContent = '"' + trimmed_out + '"';                      document.querySelector('.output_2')                .textContent = '"' + trimmed_out2 + '"';        }    </script></body>  </html>
Output:
Before clicking the button:
After clicking the button:
The trimLeft() alias: The trimStart() method has an alias which is the trimLeft() method. It performs exactly the same function as trimStart() method.
Syntax:
string.trimLeft()
Return Value: It returns the final string that is stripped out of all the white-space in the beginning.
Example: This example implements the trimLeft() method.
<!DOCTYPE html><html>  <head>    <title>        JavaScript | trimLeft() method    </title></head><body>    <h1 style="color: green">        GeeksforGeeks    </h1>          <b>        JavaScript | trimLeft() method    </b>          <pre>Output for " GeeksforGeeks     " : <span            class="output"></span>    </pre>          <pre>Output for "Portal " : <span            class="output_2"></span>    </pre>          <button onclick="trimString()">        Trim Left    </button>          <script type="text/javascript">        function trimString() {            str1 = " GeeksforGeeks     ";            str2 = "Portal ";                      trimmed_out = str1.trimLeft();            trimmed_out2 = str2.trimLeft();                      document.querySelector('.output')                .textContent = '"'                + trimmed_out + '"';            document.querySelector('.output_2')                .textContent = '"'                 + trimmed_out2 + '"';        }    </script></body>  </html>
Output:
Before clicking the button:
After clicking the button:
Supported Browsers: The browsers supported by trimStart() method are listed below:
Google Chrome 60
Firefox 61
Edge 12
Safari 12
Opera 53
javascript-functions
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?