# Remove space from string in javascript

To remove spaces from a string in JavaScript, you can use various methods depending on your needs. Here are some common approaches:

### 1\. Remove All Spaces

To remove all spaces (including spaces between words), you can use the `replace` method with a regular expression:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728985252651/17ebc3ee-c1c8-43b7-a3c3-a53d3b18f8d3.png align="center")

### 2\. Remove Leading and Trailing Spaces

To remove only leading and trailing spaces, use the `trim` method:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728985293555/422e3d74-a455-48ea-9ce8-4ca467c2c690.png align="center")

### 3\. Remove Only Spaces Between Words

To remove spaces between words but keep single spaces intact, you can use `replace` with a regular expression:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728985344546/06bd8fbd-9918-4ae4-8a69-b9e9d477afb3.png align="center")

### Summary

* Use `replace(/\s+/g, '')` to remove all spaces.
    
* Use `trim()` to remove leading and trailing spaces.
    
* Use `replace(/\s+/g, ' ').trim()` to condense multiple spaces between words into a single space.
