How to modify a string in JavaScript ?
Last Updated :
28 Apr, 2025
JavaScript strings are used for storing and manipulating text. It can contain zero or more characters within quotes and its indexing starts with 0. Strings are defined as an array of characters. In Javascript, we can also convert strings to a Character array.
Representing a String:
1. Using double quotes or single quotes.
JavaScript
<script>
// With single quotes
let string1 = 'GFG!';
console.log(string1);
// With double quotes
let string2 = "Geeks for Geeks"
console.log(string2);
</script>
Output:
GFG!
Geeks for Geeks
2. Quotes can be used inside a string, as long as they don’t match the quotes surrounding the string.
JavaScript
<script>
// Using double quotes inside a string
let string1 = 'GFG is "Awesome"!';
console.log(string1);
// Using single quotes inside a string
let string2 = "'Geeks' for 'Geeks'";
console.log(string2);
</script>
Output:
GFG is "Awesome"!
'Geeks' for 'Geeks'
3. Strings can be used as objects by using the keyword ‘new’.
JavaScript
<script>
let str = new String("GFG");
console.log(str);
</script>
Output:
[String: 'GFG']
4. We can also use special characters by using a backslash(\).
JavaScript
<script>
let string1 = "Hello, \'Sam\' is using \"Geeks for Geeks\"";
console.log(string1);
</script>
Output:
Hello, 'Sam' is using "Geeks for Geeks"
In Javascript, strings are immutable, i.e. we can't modify them using indexes. But there are some ways, we can modify a javascript string. Let's see various methods of doing it.
Method 1: We can use replace() method. The str.replace(A, B) method will return a string in which A in str has been replaced by B.
JavaScript
<script>
let string1 = "GFG is Awesome";
console.log("Before Replacing : ",string1);
let string2 = string1.replace("GFG" , "Geeks For Geeks");
console.log("After Replacing : ",string2);
</script>
Output:
Before Replacing : GFG is Awesome
After Replacing : Geeks For Geeks is Awesome
Method 2: Using the substring method to insert a character or string at a given index. string1.substring(0,pos) + 'a' + string1.substring(pos) will return a string in which 'a' is inserted at index pos in string1.
JavaScript
<script>
let string1 = "helo";
console.log(string1);
let string2 = string1.substring(0,2)
+ 'l' + string1.substring(2);
console.log(string2);
</script>
Output:
helo
hello
Method 3: By converting the string to a character array and then modifying the character array and then merging the character array to a string. To convert string to character arrays, we have 4 ways.
- split(): It returns an array by splitting the string.
- spread operator ( [...string] ): It returns an array.
- Array.from(): It returns an array from a string.
- Object.assign([], string): It returns an array by assigning string characters to it.
To convert the character array to the string we use
- s.join(""): It returns a string by joining all items of the array.
JavaScript
<script>
let name = 'GFGWebsite';
// Using split
let arr1 = name.split('');
arr1[0]='A';
let string1 = arr1.join("");
console.log(string1);
// Using spread operator
let arr2 = [...name];
arr2[1]='B';
arr2[2]='C';
let string2 = arr2.join("");
console.log(string2);
// Using Array.from
let arr3 = Array.from(name);
arr3[1]='-';
let string3 = arr3.join("");
console.log(string3);
// Using Object.assign
let arr4 = Object.assign([], name);
arr4[5]='+';
let string4 = arr4.join("");
console.log(string4);
</script>
Output:
AFGWebsite
GBCWebsite
G-GWebsite
GFGWe+site
Similar Reads
How to Concatenate Strings in JavaScript? Here are the various methods to concatenate strings in JavaScript1. Using Template Literals (Template Strings)Template literals (introduced in ES6) provide a simple way to concatenate strings. With template literals, you can embed expressions inside a string using ${} syntax. This method makes the c
3 min read
How to Convert JSON to string in JavaScript ? In this article, we are going to learn the conversion of JSON to string in JavaScript. Converting JSON to a string in JavaScript means serializing a JavaScript object or data structure represented in JSON format into a textual JSON string for data storage or transmission.Several methods can be used
3 min read
Convert Array to String in JavaScript In JavaScript, converting an array to a string involves combining its elements into a single text output, often separated by a specified delimiter. This is useful for displaying array contents in a readable format or when storing data as a single string. The process can be customized to use differen
7 min read
JavaScript - Remove Text From a String Here are the different approaches to remove specific text from a string in JavaScript, starting from the most commonly used approachesUsing replace() Method - Best MethodThe replace() method is a direct and widely used method to remove specific text by replacing it with an empty string "". This meth
2 min read
JavaScript String concat() Method The concat() method in JavaScript join or concatenate two or more strings together. It does not change the existing strings and returns a new string. This method efficiently combines multiple strings into one, ensuring the original strings remain unchanged.Syntaxstr1.concat(str2, str3, str4,......,
3 min read
JavaScript - Convert String to Array Strings in JavaScript are immutable (cannot be changed directly). However, arrays are mutable, allowing you to perform operations such as adding, removing, or modifying elements. Converting a string to an array makes it easier to:Access individual characters or substrings.Perform array operations su
5 min read
JavaScript String trim() Method The trim() method is used to remove the whitespace from both sides of a string. It does not change the original string. It is used for trimming unnecessary spaces from strings or user input data. Note: This method does not modify the original string, it returns a new trimmed string.Syntaxstr.trim();
2 min read
Remove a Character From String in JavaScript In JavaScript, a string is a group of characters. Strings are commonly used to store and manipulate text data in JavaScript programs, and removing certain characters is often needed for tasks like:Removing unwanted symbols or spaces.Keeping only the necessary characters.Formatting the text.Methods t
3 min read
How to Replace All Occurrences of a String in JavaScript? Here are different approaches to replace all occurrences of a string in JavaScript.1. Using string.replace() MethodThe string.replace() method is used to replace a part of the given string with another string or a regular expression. The original string will remain unchanged.Syntaxstr.replace(replac
2 min read
String in DSA Using JavaScript A string in JavaScript is a sequence of characters enclosed in single ('), double ("), or backticks (`). Strings in JavaScript are immutable, meaning their contents cannot be changed after creation. Any operation that modifies a string actually creates a new string.Example:JavaScriptlet s = "GfG"; c
2 min read