jQuery.trim()

Remove the whitespace from the beginning and end of a string.

jQuery.trim(str)🡢 String

str StringThe string to trim.

Note: This API has been deprecated in jQuery 3.5; please use the native String.prototype.trim method instead. Unlike jQuery.trim, String.prototype.trim does not work with types other than strings (null, undefined, Number). Make sure that your code is compatible when migrating.

The $.trim() function removes all newlines, spaces (including non-breaking spaces), and tabs from the beginning and end of the supplied string. If these whitespace characters occur in the middle of the string, they are preserved.

Remove the white spaces at the start and at the end of the string.

JS
<pre id="original"></pre>
<pre id="trimmed"></pre>
HTML
var str = "         lots of spaces before and after         ";
$("#original").html("Original String: '" + str + "'");
$("#trimmed").html("$.trim()'ed: '" + $.trim(str) + "'");
DEMO

Remove the white spaces at the start and at the end of the string.

HTML
$.trim("    hello, how are you?    ");
DEMO

Looking for a Web Developer?

👋

Hi! I'm Basti, author of this site. If you are looking for a web developer with 15+ years of experience, holla at me!

Be it the good 'ol jQuery, vanilla JS or modern frameworks like Vue and Svelte, front- or backend, I can help you.

Just write me at jobs@jqapi.com :)