JavaScript .trimStart()
The trimStart() method removes whitespace characters from the beginning of a string. It returns a new string with leading whitespace removed, leaving the original string unchanged. This method is also available as trimLeft(), which is an alias for trimStart().
Whitespace characters include spaces, tabs, line breaks, and other Unicode whitespace characters.
Syntax
string.trimStart()
Parameters:
The trimStart() method does not take any parameters.
Return value:
Returns a new string with whitespace removed from the beginning of the original string. The original string remains unchanged.
Example
The following example demonstrates how trimStart() removes leading whitespace:
const greeting = ' Hello, World!';const trimmedGreeting = greeting.trimStart();console.log(greeting);console.log(trimmedGreeting);console.log(greeting.length);console.log(trimmedGreeting.length);
The output of this code is:
Hello, World!Hello, World!1613
In this example, trimStart() removes the three leading spaces from the string, reducing its length from 16 to 13 characters.
Codebyte Example
Run the following code to see trimStart() in action:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
- Includes 34 Courses
- With Professional Certification
- Beginner Friendly.115 hours
- Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
- Beginner Friendly.15 hours