Hello Friends đź‘‹,

Welcome to Infinitbility.

Every time, when we take details from users they’re a lot of chances we will not get the same like we are expecting and space or white space is one of them.

Today, we will see how we can remove white space at the start and end of the string.

Before going to the solution, let discuss the solution that will help you on which conditions.

  • The first case, the string has both ( left and right ) side useless spaces.

    ' Hello World ' => 'Hello World'

  • The second case, the string has only useless space on the left side.

    ' Hello World' => 'Hello World'

  • The third case, the string has only useless space on the right side.

    'Hello World ' => 'Hello World'

The below solution will help you in all the above cases and it will not affect your important or original string like Hello world.

Now, move to a solution.

JavaScript provides a trim() method to remove space from the beginning and end of the string.

Let understand with an example.

let exampleString = "   Hello world    ";

console.log("exampleString => ", exampleString);



Javascript trim example
Javascript trim example

Thanks for reading…