# How To Replace All Dots In A String In JavaScript

There are many ways to replace all dots in a string in JavaScript. In this article, I will show you how to use For loop and some built-in string functions like replace(), and replaceAll(),… to do that. Let’s go into detail now.

## How To Replace All Dots in a String in JavaScript

### Use conditional statements

In Javascript, we can use For loop to solve most string problems. And now, I will use For loop to replace all dots in a string.

Example:

var myString = "This.is.a.string";
var newString = '';

for (let i = 0 ; i < myString.length ; i++)

{
if(myString[i] === '.'){
newString = newString + " ";
}
else
newString = newString + myString[i];
}

console.log(newString);

Output:

This is a string

In the above example, I changed all the dots in the string myString to a space character by For loop combined with an if conditional to check which character is a period. Then I replaced it with a space, and we no longer see any more dots in that string.

### Use replace() method

Replace() method is a built-in function that can replace any string with the desired string. To replace all dots in a string, we have to use regular expressions while searching for any string so we can do what we want.

Syntax:

string.replace(searchValue, newValue)

Parameters – Description:

• searchValue: is the string that you want to replace with another string.
• newValue: is the replacement string you want.

Example:

var myString = "This.is.a.string";
var newString = myString.replace(".", " ");

console.log(newString);

Output:

This is.a.string

The above example shows that if we use the replace() method in the usual way, it only replaces a string, then the first one it finds. So if we want to replace all dots in a string, we have to use more regular expressions. I do it like this:

var myString = "This.is.a.string";
var newString = myString.replace(/\./g,' ');

console.log(newString);

Output:

This is a string

After adding the regular expression, we see that our program has output to the console precisely as we want.

### Use replaceAll() method

ReplaceAll(), the exact mechanism of action as replace() is also to get a string, any characters will be replaced with the character string you want, but the difference is that the replaceAll() method can replace the entire string of characters found without need a regular expression like replace() method.

Syntax:

string.replaceAll(searchValue, newValue)

Parameters – Description:

• searchValue: is the string that you want to replace with another string.
• newValue: is the replacement string you want.

Example:

var myString = "This.is.a.string";
var newString = myString.replaceAll(".", " ");

console.log(newString);

Output:

This is a string

## Summary

In this article, I have helped you with some ways to replace all dots in a string in JavaScript. In my opinion, these are pretty easy to remember and easy to use. I hope it will help you and your program. Good luck.

Maybe you are interested: