;

How To Change String Value Using Replace in Javascript

Replace Method - Changing String in Javascript


How To Change String Value Using Replace in Javascript

buayaberdiri.blogspot.com - In Javascript we can change the text string stored in the variable. To change text strings in javascript, we can use the string method in javascript, namely replace().

replace() is a string method in javascript that is used to change the string for a particular sentence.

How to use it ?, see the sample code below:


let data = "Buaya Terbang Blog";
console.log(data.replace("Terbang", "Berdiri"));


In the code example above, we make the value of the data variable "Buaya Terbang Blog", then we want to change the text "Terbang" to "Berdiri". Then we can use the code data.replace("Terbang", "Berdiri").

Then the Console on the browser will display the change in the value of that variable.


We can also change strings containing numeric values, Javascript is very flexible to do this. See the sample code below:


let data = "1010 1111";
console.log(data.replace("1111", "2222"));


In the code example above, we want to change the value of the string number 1111 to 2222. And the console on the browser will display the change to string 0101 2222.



Post articles about Javascript that you can read:







List of Article Posts https://buayaberdiri.blogspot.com