javascript remove spaces at the beginning of the end of the string 1

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'

Here is what the above code is Doing:
1. We have a string with spaces at the beginning and at the end.
2. We use the trimStart() method to remove the spaces at the beginning of the string.
3. We use the trimEnd() method to remove the spaces at the end of the string.
4. We use the trim() method to remove the spaces at the beginning and at the end of the string.

Similar Posts