Trimming input values of form

VANDANA GUPTA
2 min readAug 1, 2021

While validating form inputs, you must have encountered a situation where we must have got input values with a lot of spaces.

Like this ⬇

console.log(username)
>' ' // -> lot of spaces

Or maybe like this ⬇

But hold on 🔴

Take a cup of coffee ☕

Let’s together see different methods that our dear JavaScript provides to stop this from happening.

TrimStart and TrimEnd

trimStart() and trimEnd() removes the starting and ending whitespaces respectively created by the user on entering the values.

Let's now use the power of both of the above methods by chaining the methods to solve our problem.

Kudos🎊, we succeeded in doing it.Lets go ahead with other alternatives.

TrimLeft and TrimRight

trimLeft() and trimRight() removes the left and right white spaces respectively.

Chaining both the methods as below solves the problem.

Trim

trim() methods removes the whitespaces from both the ends of the string.

Let’s recall the points :

  1. trimLeft() and trimStart() removes the whitespaces from the start of the string
  2. trimRight() and trimEnd() removes the whitespaces from the end of the string
  3. trim() removes the whitespaces from both the ends of the string.
Venn diagram for different trim methods

I hope you find it useful and hope you will use it wherever you find its use.

Thank you ✨✨

Happy Coding !!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

VANDANA GUPTA
VANDANA GUPTA

Written by VANDANA GUPTA

An inquisitive person. A learner.

No responses yet

Write a response