Skip to content
Latest: Publish JavaScript Packages to NPM Like a Pro!

trimStart() in JavaScript – Remove Whitespace from String's Beginning

Whenever you use trimStart() on a string, the method does the following:

  1. It trims whitespace at the start of the string.
  2. It returns the new version of the calling string—without changing the original string.

trimStart() accepts no arguments. Here is the syntax:

callingString.trimStart();

Example: Use trimStart() to Remove Whitespace at the Beginning of a String

Section titled “Example: Use trimStart() to Remove Whitespace at the Beginning of a String”
const myColor = " I love blue. ";
// Remove whitespace characters at the beginning of myColor:
myColor.trimStart();
// The invocation above will return: "I love blue. "

Try it on CodePen

Note that you can alternatively use replace() and regular expression to implement trimStart()’s functionality.

Example: Use replace() and Regular Expression to Remove Whitespace at the Beginning of a String

Section titled “Example: Use replace() and Regular Expression to Remove Whitespace at the Beginning of a String”
const myName = " My name is Oluwatobi. ";
// Trim away the whitespace at the beginning of myName:
trimStartWhitespace(myName);
function trimStartWhitespace(string) {
return string.replace(/^\s+/gm, "");
}
// The invocation above will return: "My name is Oluwatobi. "

Try it on CodePen