remove spaces from beginning and end of string javascript code example

Example 1: remove spaces in a string js

str.replace(/\s+/g, '')

Example 2: javascript remove all whitespaces

var spacesString= "Do I have spaces?"; 
var noSpacesString= myString.replace(/ /g,'');// "DoIhavespaces?"

Example 3: javascript remove all spaces from string

str = str.replace(/\s/g, '');

Example 4: remove space from string javascript

var str = "       Hello World!        ";
alert(str.trim());

Example 5: javascript remove spaces at the beginning of the end of the string

const stringWithSpaces = '   I learn JS with Coderslang every day   ';

console.log(stringWithSpaces.trimStart());  //'I learn JS with Coderslang every day   '
console.log(stringWithSpaces.trimEnd());    //'   I learn JS with Coderslang every day'
console.log(stringWithSpaces.trim());       //'I learn JS with Coderslang every day'