%% generate tags start %%
#software-engineering
%% generate tags end %%
#software-engineering/javascript
In JavaScript, the `replace` and `replaceAll` methods are used to replace parts of a string with new values.
The main difference between them is how they deal with the replacement operation:
- `replace` replaces only the first occurrence of the specified pattern with the provided replacement. If the pattern appears multiple times in the string, only the first occurrence is replaced.
- `replaceAll` replaces all occurrences of the specified pattern with the provided replacement. It replaces every instance of the pattern in the string.
Here's an example to illustrate the difference:
```js
const string = 'Hello, hello, hello!';
const replaced = string.replace('hello', 'Hi');
const replacedAll = string.replaceAll('hello', 'Hi');
console.log(replaced); // Output: "Hi, hello, hello!"
console.log(replacedAll); // Output: "Hi, Hi, Hi!"
```
In the example, `replace` only replaces the first occurrence of "hello" with "Hi", while `replaceAll` replaces all occurrences of "hello" with "Hi" in the string.