- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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