How to trim a string using $.trim() in jQuery?

To trim a string in jQuery, use the trim() method. It removes the spaces from starting and end of the string.

The sample string I am using has spaces −

var myStr = "  Hello World    ";

Use the trim() method, 


The following is an example to trim a string in jQuery −


Live Demo

<!DOCTYPE html>
<script src=""></script>
    var myStr = "  Hello World    ";
    myStr = jQuery.trim(myStr);

<button id="button1">Trim</button>


Updated on: 15-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started