String.prototype.trimStart()
trimStart() string · Added in vES2019 · Updated March 16, 2026 · String Methods The trimStart() method removes whitespace characters from the beginning of a string and returns a new string. Unlike trim(), which removes whitespace from both ends, trimStart() only targets leading whitespace — making it useful when you need to preserve trailing whitespace or handle asymmetrically-padded strings.
String Immutability
Strings in JavaScript are immutable — they cannot be changed after creation. Every string method that appears to modify a string actually returns a new one. This applies to trimStart() as well:
const text = " hello";
const cleaned = text.trimStart();
console.log(text); // " hello" (unchanged)
console.log(cleaned); // "hello" (new string)
This immutability is a fundamental characteristic of JavaScript strings. Always assign the returned value to a variable if you need the trimmed result.
Whitespace Characters
The trimStart() method removes various leading whitespace characters, including:
- Space (
) - Tab (
\t) - Newline (
\n,\r\n) - Non-breaking space (
\u00A0) - Other Unicode whitespace from the White_Space category
// Various leading whitespace characters
const messy = "\t\n hello world\u00A0\n";
console.log(messy.trimStart()); // "hello world\u00A0\n"
Note that trimStart() only removes leading (left-side) whitespace, preserving any whitespace within the string or at the end.
Why trimStart() Over trimLeft()?
trimLeft() is a legacy alias that behaves identically to trimStart(). However, trimStart() is the standardized method name and is preferred for several reasons:
- Consistency: The
Start/Endnaming aligns with other modern methods likepadStart()andpadEnd(). - Clarity:
trimStart()clearly indicates the direction (left = beginning), whiletrimLeft()can be ambiguous in right-to-left (RTL) languages. - Future-proofing: New code should use the standardized method to avoid deprecated alias warnings.
// Both methods work identically
" hello".trimLeft(); // "hello"
" hello".trimStart(); // "hello"
// They reference the same function
console.log("".trimLeft === "".trimStart); // true
Use Cases
Standardizing user input
When users paste or type text, leading whitespace often appears accidentally:
function normalizeInput(input) {
return input.trimStart();
}
console.log(normalizeInput(" john")); // "john"
console.log(normalizeInput("\tpassword")); // "password"
console.log(normalizeInput("\n\ndata")); // "data"
Parsing log files
Log files often have timestamps or indentation at the start of each line:
const logLines = [
" [INFO] Application started",
" [DEBUG] Loading config",
" [ERROR] Connection failed"
];
const cleaned = logLines.map(line => line.trimStart());
console.log(cleaned);
// ["[INFO] Application started", "[DEBUG] Loading config", "[ERROR] Connection failed"]
Handling multi-line strings
When working with template literals, leading whitespace from indentation is common:
const template = `
Hello,
World!
`;
console.log(template.trimStart());
// "Hello,\n World!\n"
Browser Compatibility
trimStart() (and its alias trimLeft()) is supported in all modern browsers and Node.js 10+. It was standardized in ES2019, though many browsers supported it earlier under vendor prefixes. For older environments, use a polyfill:
if (!StringtrimStart) {
StringtrimStart = function() {
return this.replace(/^\s+/, '');
};
}
Key Behaviors
- Returns a new string; original string is unchanged (immutability)
- Removes whitespace from the beginning only, preserving trailing whitespace
- Whitespace includes spaces, tabs, newlines, and Unicode whitespace
- If no whitespace exists at the start, returns the original string unchanged
trimLeft()is a legacy alias; prefertrimStart()in new code
See Also
- String.trim() — Remove whitespace from both ends
- String.trimEnd() — Remove whitespace from the end