JavaScript - trimEnd() method

revision:


Category : string

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

This methodworks like trim(), but removes whitespace only from the end of a string.

Syntax :

        string.trimEnd()
    

Parameters: none

Examples:

        <p>trimEnd() removes whitespace from the end of a string:</p>
        <pre><;p id="demo"></p></pre>
        <script>
            let text1 = "     Hello World!     ";
            let text2 = text1.trimEnd();
            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.trimEnd();
                        document.getElementById("trim-2").innerHTML = "Length text1 = " + text1.length + "<br>Length text2 = " + text2.length;;
                        
                    </script>