JavaScript String trimStart() Method

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Instance method

What You’ll Learn

String.prototype.trimStart() returns a new string with whitespace removed from the start only (same API as MDN String.prototype.trimStart()). Learn the trimLeft alias, how it differs from trim() / trimEnd(), five examples, and try-it labs.

01

Kind

Instance method

02

Returns

New string

03

Parameters

None

04

Removes

Start only

05

Alias

trimLeft()

06

Baseline

Widely available

Introduction

Sometimes you need to drop leading spaces, tabs, or indentation but keep trailing padding. That is exactly what trimStart() does — clean the left side only.

After trim() was standardized, engines also had a non-standard trimLeft(). The standard name is trimStart() (to match padStart()), and trimLeft remains as an alias of the same function for compatibility.

💡
Beginner tip

Prefer trimStart() in new code. Use plain trim() when both ends should be cleaned (typical form fields).

This page is part of JavaScript String Methods. Related topics include trim() and trimEnd().

Understanding the trimStart() Method

str.trimStart() builds a new string with leading whitespace removed. Trailing whitespace and middle spaces stay unchanged.

  • Returns a new string — the original is unchanged.
  • Takes no parameters.
  • Only the start (left side) is cleaned.
  • trimLeft is the same function (alias).

📝 Syntax

General form of String.prototype.trimStart:

JavaScript
str.trimStart()
str.trimLeft()  // alias — same function

Parameters

None.

Return value

A new string representing str stripped of whitespace from its beginning. If there is no leading whitespace, a new string is still returned (essentially a copy).

Exceptions

None for normal string values.

Aliasing

trimLeft and trimStart refer to the exact same function object. In some engines, String.prototype.trimLeft.name === "trimStart".

Common patterns

JavaScript
"   Hello world!   ".trimStart();  // "Hello world!   "
"   foo  ".trimStart();            // "foo  "
"\t hi \n".trimStart();            // "hi \n"
"no-spaces".trimStart();           // "no-spaces"

⚡ Quick Reference

GoalCode
Trim the startstr.trimStart()
Old alias namestr.trimLeft()
Trim both endsstr.trim()
Trim the endstr.trimEnd()
Drop leading indentline.trimStart()

🔍 At a Glance

Four facts to remember about String.trimStart().

Returns
string

New trimmed text

Side
start

Left side only

Alias
trimLeft

Same function

Mutates
no

Immutable

📋 trimStart() vs trim() vs trimEnd()

trimStart()trim()trimEnd()
Removes fromStart onlyBoth endsEnd only
AliastrimLeft()trimRight()
" hi ""hi ""hi"" hi"
Best forLeading spaces / indentForm fieldsTrailing spaces / newlines

Examples Gallery

Examples follow MDN String.trimStart() patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

MDN demos for trimming the start only.

Example 1 — Basic trimStart()

MDN Try it demo: leading spaces removed; trailing spaces kept.

JavaScript
const greeting = "   Hello world!   ";

console.log(greeting);
// Expected output: "   Hello world!   ";

console.log(greeting.trimStart());
// Expected output: "Hello world!   ";
Try It Yourself

How It Works

Only the spaces before Hello are removed. The three spaces after ! remain.

Example 2 — Length After trimStart()

MDN Using trimStart() demo — watch the length change.

JavaScript
let str = "   foo  ";

console.log(str.length); // 8

str = str.trimStart();
console.log(str.length); // 5
console.log(str);        // 'foo  '
Try It Yourself

How It Works

Three leading spaces are removed (8 → 5). The two trailing spaces stay, so the string still ends with padding.

🔧 Details & Patterns

Alias, family compare, and de-indenting lines.

Example 3 — trimLeft Alias

Both names call the same function.

JavaScript
const padded = "  hi  ";

console.log(padded.trimStart() === padded.trimLeft());
// true

console.log(String.prototype.trimStart === String.prototype.trimLeft);
// true

console.log(String.prototype.trimLeft.name);
// "trimStart" (in many engines)
Try It Yourself

How It Works

Write trimStart in new tutorials and apps. Older code may still say trimLeft — it is fine, just an alias.

Example 4 — Family Side-by-Side

Same input, three different trim methods.

JavaScript
const padded = "  hi  ";

console.log(JSON.stringify(padded.trimStart()));
// "hi  "

console.log(JSON.stringify(padded.trim()));
// "hi"

console.log(JSON.stringify(padded.trimEnd()));
// "  hi"
Try It Yourself

How It Works

Pick the method that matches which edge(s) you want cleaned. Default to trim() for user input.

Example 5 — Drop Leading Indent, Keep Trailing Pad

Practical pattern: remove left indent while preserving end spacing.

JavaScript
function deindent(line) {
  return line.trimStart();
}

const padded = "    value  ";
const cleaned = deindent(padded);

console.log(JSON.stringify(cleaned));
// "value  "

console.log(cleaned.startsWith(" ")); // false — leading gone
console.log(cleaned.endsWith("  "));  // true — trailing kept
Try It Yourself

How It Works

Using full trim() here would also remove the trailing spaces. trimStart() only removes the leading indent.

🎯 Common Use Cases

  • Remove indentation — strip leading spaces/tabs while keeping end padding.
  • Fixed-width fields — drop left pad, keep right alignment spaces.
  • Pasted text cleanup — remove accidental leading whitespace only.
  • Not for form fields — usually prefer trim() (both ends).
  • Not mid-string spaces — use replace / regex for those.

🧠 How trimStart() Works

1

Scan from the start

Walk forward while characters are whitespace.

Start
2

Stop at first non-space

Everything after that point is kept, including trailing spaces.

Keep
3

Drop the leading run

Spaces, tabs, and line breaks at the start are removed.

Trim
4

Return a new string

Original string is never modified.

📝 Notes

  • Always returns a new string — assign it if you need the result.
  • Trailing whitespace is intentionally kept.
  • trimLeft is an alias — prefer trimStart in new code.
  • Does not change spaces in the middle of the string.
  • Named to mirror padStart() after standardization.

Browser & Runtime Support

String.prototype.trimStart() is Baseline Widely available — supported across modern browsers since January 2020.

Baseline · Widely available

String.trimStart()

Safe for production in browsers and runtimes (Node.js, Deno, Bun). Prefer trimStart() over the trimLeft() alias in new code.

Universal Widely available
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
trimStart() Excellent

Bottom line: Use String.trimStart() to remove leading whitespace while keeping trailing spaces. Use trim() for both ends and trimEnd() for the end only.

Conclusion

String.prototype.trimStart() cleans the left side of a string — perfect when trailing padding must stay. Prefer it over trimLeft(), and use trim() when both ends should go.

Continue with trim(), trimEnd(), or the String methods hub.

💡 Best Practices

✅ Do

  • Use trimStart when trailing spaces must remain
  • Prefer trimStart over trimLeft in new code
  • Use trim() for typical form input
  • Assign the return value — original stays padded
  • Pair mentally with padStart naming

❌ Don’t

  • Expect trailing spaces to disappear
  • Use it when both ends should be cleaned
  • Assume mid-string spaces are collapsed
  • Forget tabs and newlines also count as whitespace
  • Mutate expectations without assignment

Key Takeaways

Knowledge Unlocked

Five things to remember about String.trimStart()

Start only — trailing spaces kept, trimLeft is an alias.

5
Core concepts
📏 02

Removes

start only

Role
🔄 03

Alias

trimLeft

Compat
🔍 04

Keeps

trailing spaces

Rule
05

Mutates

no

Immutable

❓ Frequently Asked Questions

String.prototype.trimStart() returns a new string with whitespace removed from the beginning (left side) only. Trailing whitespace is kept. The original string is not changed.
Yes. trimLeft() is an alias of trimStart() — they are the same function. Prefer trimStart() for consistency with padStart().
Whitespace includes spaces, tabs, newlines, and other white space characters plus line terminators (same rules as trim()).
trim() cleans both ends. trimStart() removes only from the start. trimEnd() removes only from the end.
No. Strings are immutable. trimStart() always returns a new string — even when there was nothing to remove.
When you must keep trailing padding or spaces but want to drop leading spaces, tabs, or indentation — for example aligning text while preserving end padding.
Did you know?

The name trimStart was chosen to match padStart. trimLeft stayed as an alias so older pages and libraries keep working — in many engines even trimLeft.name reports "trimStart".

More String Methods

Return to the hub for slice, trim, replace, and more.

String methods hub →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful