References

Beginner-friendly references for web development, with live, editable examples.

The JavaScript string.trimStart() method

Method JavaScript All modern browsers Updated
Quick answer

The trimStart() method removes whitespace from the start of a string and returns a new string. " hi ".trimStart() gives "hi " — only the leading spaces go. It's the start-only version of trim(); its counterpart trimEnd() handles the end.

Overview

trimStart() strips whitespace — spaces, tabs, newlines — from the beginning of a string, leaving the end untouched. It returns a new string (strings are immutable), so you use the return value.

It's one of a trio with trim() (both ends) and trimEnd() (the end only). Most of the time you want trim(), but trimStart() is the right tool when trailing whitespace is meaningful — preserving alignment or intentional trailing content while cleaning up leading indentation.

It's occasionally seen under its older alias trimLeft(), which still works but is non-standard; prefer trimStart(). As with the others, it only touches the edges — to remove whitespace inside a string, use a regex replace().

Syntax

str.trimStart()

"  hello  ".trimStart()  // "hello  "

Example

Live example
<pre id="out" style="font:14px ui-monospace,monospace"></pre>
<script>
  const raw = '    indented line';

  document.getElementById('out').textContent =
    '[' + raw + ']\n' +
    '[' + raw.trimStart() + ']'; // leading spaces removed
</script>

Best practices

  • Use trimStart() when only leading whitespace should go and trailing whitespace matters.
  • For both ends, use trim(); for the end only, trimEnd().
  • Use the return value — the original string is unchanged.
  • Prefer trimStart() over the non-standard alias trimLeft().

Frequently asked questions

What does trimStart() do?
It removes whitespace from the start of a string and returns a new string, leaving the end as is.
What is the difference between trimStart() and trim()?
trimStart() removes whitespace only from the beginning; trim() removes it from both ends.
Is trimLeft() the same as trimStart()?
Yes — trimLeft() is a legacy alias. Use the standard trimStart().
How do I trim the end of a string?