Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

String.prototype.trimStart()

BaselineWidely available

ThetrimStart() method ofString values removes whitespace from the beginning of this string and returns a new string, without modifying the original string.trimLeft() is an alias of this method.

Try it

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

Syntax

js
trimStart()trimLeft()

Parameters

None.

Return value

A new string representingstr stripped of whitespace from its beginning (left side). Whitespace is defined aswhite space characters plusline terminators.

If the beginning ofstr has no whitespace, a new string is still returned (essentially a copy ofstr).

Aliasing

Aftertrim() was standardized, engines also implemented the non-standard methodtrimLeft. However, for consistency withpadStart(), when the method got standardized, its name was chosen astrimStart. For web compatibility reasons,trimLeft remains as an alias totrimStart, and they refer to the exact same function object. In some engines this means:

js
String.prototype.trimLeft.name === "trimStart";

Examples

Using trimStart()

The following example trims whitespace from the start ofstr, but not from its end.

js
let str = "   foo  ";console.log(str.length); // 8str = str.trimStart();console.log(str.length); // 5console.log(str); // 'foo  '

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-string.prototype.trimstart

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp