Skip to main content

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.
  • A calling string is a string on which you used trimStart(). So, in " Hello, world! ".trimStart(), " Hello, world! " is the calling string.
  • Whitespace means the space character, tab, carriage return, new line, form feed, vertical tab, and other Unicode whitespace characters.
  • trimLeft() is an alias for trimStart(). So, you can technically use the two methods to trim whitespace at the start of a string. However, it's best practice to use trimStart().
  • trimStart() is sometimes written as String.prototype.trimStart() because it is a method of the String object's prototype property.

Syntax of the trimStart() Method

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


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:

// 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

const myName = "          My name is Oluwatobi.          ";

// Trim away the whitespace at the beginning of myName:

function trimStartWhitespace(string) {
return string.replace(/^\s+/gm, "");

// The invocation above will return: "My name is Oluwatobi. "

Try it on CodePen

  • Use trimEnd() to remove whitespace only at the end of a string.
  • Use trim() to remove whitespace from both ends of a string.


The JavaScript trimStart() method trims whitespace only at the beginning of a string.

Join CodeSweetly's Newsletter