javascript remove space from start and end of string code example

Example 1: js remove space from string

string.split(" ").join("")

Example 2: how to remove spaces from strings javascript

var str = '/var/www/site/Brand new document.docx';

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

Example 3: 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'

Example 4: delete space from string javascript

var str = "470 ";
str.trim();