Removing space in string in Javascript

Learn how to remove space in string.

If you want to remove all the space in the string thenΒ , you can use replace method

var name = "Javascript jeep  ";
name.replace(/s/g,''); // Javascriptjeep
s --> space
g --> replace globally
We can also replace the space with '-'
var name = "Javascript jeep  ";
name.replace(/s/g,'-'); // Javascript-jeep--

If you want to remove the extra space at the end of the string then you can use

var s = "string   ";
s = s.trimRight();    //string
//trimRight() returns a new string, removing the space on end of the string

If you want to remove the extra space at the beginning of the string then you can use

var s = "    string";
s = s.trimLeft();    //string
//trimLeft() returns a new string, removing the space on start of the string

If you want to remove the extra space at the both end of the string then you can use

var s = "   string   ";
s = s.trim();    //string
//trim() returns a new string, removing the space on start and end of the string

If you find this helpful surprise 🎁 me here.

Share if you feel happy.

Follow Javascript JeepπŸš™ if you feel worthy.

Leave a comment

Design a site like this with WordPress.com
Get started