JavaScript String trimStart() Method
The JavaScript String trimStart() method removes whitespace characters from the beginning (left side) of the current string. It returns a new string without modifying the original string. If the beginning of the current string has no whitespace characters, the new string is the same as the original string
To remove the whitespaces from both ends or only from right end, we have other methods such as − trim() and trimEnd().
Syntax
Following is the syntax of JavaScript String trimStart() method −
trimStart()
Parameters
- It does not accept any parameters.
Return value
This method returns a new string with whitespace characters removed from the beginning (left side) of the current string.
Example 1
If the beginning (left side) of the current string has no whitespace characters, this method returns the original string unchanged.
<html>
<head>
<title>JavaScript String trimStart() Method</title>
</head>
<body>
<script>
const str = "Tutorials Point";
document.write("Original string: ", str);
document.write("<br>New string: ", str.trimStart());
</script>
</body>
</html>
Output
The above program returns "Tutorials Point".
Original string: Tutorials Point New string: Tutorials Point
Example 2
In this example, we use the trimStart() method to remove the whitespace from the beginning of the string " Hello World! ".
<html>
<head>
<title>JavaScript String trimStart() Method</title>
</head>
<body>
<script>
const str = " Hello World! ";
document.write("Original string: ", str);
document.write("<br>Length of original string: ", str.length);
document.write("<br>New string: ", str.trimStart());
document.write("<br>Length of new string: ", str.trimStart().length);
</script>
</body>
</html>
Output
After executing the above program, it trimmed the white spaces from the beginning of the string and returned a new string "Hello World!".
Original string: Hello World! Length of original string: 14 New string: Hello World! Length of new string: 13