 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Does JavaScript have a method to replace part of a string without creating a new string?
Yes, we can replace part of a string without creating a new string using the replace() method as in the below syntax −
var anyVariableName=yourValue; yourVariableName=yourVariableName.replace(yourOldValue,yourNewValue);
Example
var message="Hello,this is John"; console.log("Before replacing the message="+message); message=message.replace("John","David Miller"); console.log("After replacing the message="+message); To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo57.js.
Output
This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo57.js Before replacing the message=Hello,this is John After replacing the message=Hello,this is David Miller
Advertisements
 