JavaScript - trimStart() method

revision:


Category : string

The trimStart() method removes whitespace from the beginning of a string. It does not change the original string.

This method works like trim(), but removes whitespace only from the start of a string.

Syntax :

        string.trimStart()
    

Parameters: none

Examples:

            <p>trimStart() removes whitespace from the start of a string:</p>
            <pre><;p id="demo"></p></pre>
            <script>
                let text1 = "     Hello World!     ";
                let text2 = text1.trimStart();
                document.getElementById("demo1").innerHTML = "Length text1 = " + text1.length + "<br>Length text2 = " + text2.length;
            </script>
        

Practical examples

example: remove spaces

code:
                    <div>
                        <p id="trim-1"></p>
                        <p id="trim-2"></p>
                        <p id="trim-3"></p>
                        
                    </div>
                    <script>
                        let text1 = "     Hello World!     ";
                        document.getElementById("trim-1").innerHTML = "untrimmed text : " + text1;
                        let text2 = text1.trimStart();
                        document.getElementById("trim-2").innerHTML = "Length text1 = " + text1.length + "<br> Length text2 = " + text2.length;;
                        
                    </script>