Trimming input values of form

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

Like this ⬇

>' ' // -> 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() 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 !!




An inquisitive person. A learner.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Functional Programming: Closures and Memoization in JavaScript

Sample JavaScript image

Routing in React js React-Router

Let's Understand the Express.js Framework

You Probably Don’t Need React/Redux

Daily Journal #008 — Nov. 15th, 2018

Next.js & Statamic 3.3 headless preview

Neo4j Performance — Tips for beginners

Functional JavaScript: Writing a Simple Command-line Game (Part 2)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


An inquisitive person. A learner.

More from Medium

TYPO3 Tricks and Tips

Trip to Grand Canyon

Tableau Level of Detail Expressions [Simplified]

Sorting Algorithms Part 2: Insertion Sort