Horje Website
Trim whitespace

To trim whitespace from the edges of a string, use String.prototype.trim:

some whitespaced string “.trim(); // “some whitespaced string”

Many JavaScript engines, but not Internet Explorer, have implemented non-standard trimLeft and trimRight methods. There is a proposal, currently at Stage 1 of the process, for standardised trimStart and trimEnd methods, aliased to trimLeft and trimRight for compatibility.

// Stage 1 proposal

this is me

this is me

“.trimStart(); // “this is me “

“.trimEnd(); // “

this is me”

// Non-standard methods, but currently implemented by most engines

this is me

this is me

“.trimLeft(); // “this is me

“.trimRight(); // “

this is me”

December 31, 2022
JavaScript/jQuery How to

This article was posted in JavaScript/jQuery How to. Bookmark the permalink. Follow comments with the RSS feed for this post.Post a Comment or leave a trackback: Trackback URL.

Leave a Reply

Your email address will not be published. Required fields are marked *


Horje © 2011 - 2023