JavaScript > String Methods

Trim in JavaScript

How to trim the white space from a string in JavaScript?


Trim

Trim is nothing but removing, and it is a string method which is used to remove the white space from both the sides of a string. White space is nothing but the space, tab, no-break space, etc.., This method does not affect to the value of a string.

Trim Functions

The concept Trim produces three functions/methods in JavaScript, they are as follow.

  • trim() method - Removes whites pace both sides of a string.
  • trimLeft() method - Removes white space left side of a string.
  • trimRight() method - Removes white space right side of a string.

String Trim

In JavaScript By using trim() method we can trim the string white space from both the sides.

<p>Click the below button to remove the white space.</p>
<input type="button" value="Trim" onclick="Function()" />

<script>
    function Function() {
        var A = " TechFunda & DotNetFunda ";
        alert(A.trim());
    }
</script>

In the above code snippet we have given the trim() method to the variable A in the <script> block. There is a button "Trim" in the HTML code, onclick of the button fires the Function() in the <script> code, at the same time string method trim() remove whitespace from both sides of string and returns the output as alert, because we have given alert(A.trim());

OUTPUT

Left Trim

Left Trim {trimLeft() method} removes the left side whit space of a string value.

<p>Click the below button to remove the left side white space.</p>
<input type="button" value="Trim" onclick="Function()" />

<script>
    function Function() {
        var A = "  TechFunda        ";
        alert(A.trimLeft());
    }
</script>

Left Trim

In the above code snippet we have given trimLeft() method which is used to remove the leftside whitespace of the variable A value. Onclick of the button "Trim" in the HTML code fires the function Function() in the <script> block, at the same time trimLeft() method trims the left side white space and returns the output as alert.

OUTPUT

In the above output we can notice that, the trimLeft() method removes the left side white space of the variable A value. So that the variable A value "TechFunda" is appearing at the start position in the alert.

Right Trim

Right Trim {trimRight() method} removes the right side whit space of a string value.

<p>Click the below button to remove the right side white space.</p>
<input type="button" value="Trim" onclick="Function()" />

<script>
    function Function() {
        var A = "        TechFunda ";
        alert(A.trimRight());
    }
</script>

Right Trim

In the above code snippet we have given trimRight() method which is used to remove the right side whitespace of the variable A value. Onclick of the button "Trim" in the HTML code fires the function Function() in the <script> block, at the same time trimRight() method trims the right side white space and returns the output as alert.

OUTPUT

In the above output we can notice that, the trimRight() method removes the right side white space of the variable A value. So that the variable A value "TechFunda" is not appearing at the start position in the alert, that means the left side white space still remains there in the variable A.


 Views: 1272 | Post Order: 80



Write for us