Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions content/javascript/concepts/strings/terms/trimStart/trimStart.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
Title: '.trimStart()'
Description: 'Removes whitespace characters from the start (left side) of a string.'
Subjects:
- 'Computer Science'
- 'Web Development'
Tags:
- 'Methods'
- 'Strings'
CatalogContent:
- 'introduction-to-javascript'
- 'paths/front-end-engineer-career-path'
---

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

```pseudo
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:

```js
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:

```shell
Hello, World!
Hello, World!
16
13
```

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:

```codebyte/javascript
// Example with various whitespace characters
const text1 = " JavaScript";
const text2 = "\t\tProgramming";
const text3 = "\n\nCoding";

console.log("Original:", text1);
console.log("Trimmed:", text1.trimStart());

console.log("Original:", text2);
console.log("Trimmed:", text2.trimStart());

console.log("Original:", text3);
console.log("Trimmed:", text3.trimStart());

// Example showing that trailing spaces are preserved
const text4 = " Both sides ";
console.log("Original length:", text4.length);
console.log("After trimStart length:", text4.trimStart().length);
```