
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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
- Related Articles
- Replace Character in a String in Java without using replace() method
- Replace part of a string with another string in C++
- Replace words of a string - JavaScript
- String function to replace nth occurrence of a character in a string JavaScript
- Replace All Occurrences of a Python Substring with a New String?
- Does Python have a string 'contains' substring method?
- Create a polyfill to replace nth occurrence of a string JavaScript
- How to replace a part of the string (domain name after @) using MySQL?
- Creating String Object from certain part of a character Array in Java
- Can part of a string be rearranged to form another string in JavaScript
- How to replace all occurrences of a string in JavaScript?
- MySQL query to replace part of string before dot
- Replace part of string in MySQL table column?
- JavaScript - Writing a string function to replace the kth appearance of a character
- Creating all possible unique permutations of a string in JavaScript

Advertisements