The JavaScript string.trimStart() method
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 and is kept in the spec only for web compatibility; 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
<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
Frequently asked questions
What does trimStart() do?
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()?
trimLeft() is a legacy alias. Use the standard trimStart().