JavaScript's implementation of ECMAScript can vary from browser to browser, however for Chrome, many string operations (substr, slice, regex, etc.) simply retain references to the original string rather than making copies of the string. This is a known issue in Chrome (Bug #2869). To force a copy of the string, the following code works:

var string_copy = (' ' + original_string).slice(1);

This code works by appending a space to the front of the string. This concatenation results in a string copy in Chrome's implementation. Then the substring after the space can be referenced.

This problem with the solution has been recreated here: http://jsfiddle.net/ouvv4kbs/1/

WARNING: takes a long time to load, open Chrome debug console to see a progress printout.

// We would expect this program to use ~1 MB of memory, however taking
// a Heap Snapshot will show that this program uses ~100 MB of memory.
// If the processed data size is increased to ~1 GB, the Chrome tab
// will crash due to running out of memory.

function randomString(length) {
  var alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  var result = '';
  for (var i = 0; i < length; i++) {
    result +=
        alphabet[Math.round(Math.random() * (alphabet.length - 1))];
  }
  return result;
};

var substrings = [];
var extractSubstring = function(huge_string) {
  var substring = huge_string.substr(0, 100 * 1000 /* 100 KB */);
  // Uncommenting this line will force a copy of the string and allow
  // the unused memory to be garbage collected
  // substring = (' ' + substring).slice(1);
  substrings.push(substring);
};

// Process 100 MB of data, but only keep 1 MB.
for (var i =  0; i < 10; i++) {
  console.log(10 * (i + 1) + 'MB processed');
  var huge_string = randomString(10 * 1000 * 1000 /* 10 MB */);
  extractSubstring(huge_string);
}

// Do something which will keep a reference to substrings around and
// prevent it from being garbage collected.
setInterval(function() {
  var i = Math.round(Math.random() * (substrings.length - 1));
  document.body.innerHTML = substrings[i].substr(0, 10);
}, 2000);

Answer from AffluentOwl on Stack Overflow
Top answer
1 of 11
99

JavaScript's implementation of ECMAScript can vary from browser to browser, however for Chrome, many string operations (substr, slice, regex, etc.) simply retain references to the original string rather than making copies of the string. This is a known issue in Chrome (Bug #2869). To force a copy of the string, the following code works:

var string_copy = (' ' + original_string).slice(1);

This code works by appending a space to the front of the string. This concatenation results in a string copy in Chrome's implementation. Then the substring after the space can be referenced.

This problem with the solution has been recreated here: http://jsfiddle.net/ouvv4kbs/1/

WARNING: takes a long time to load, open Chrome debug console to see a progress printout.

// We would expect this program to use ~1 MB of memory, however taking
// a Heap Snapshot will show that this program uses ~100 MB of memory.
// If the processed data size is increased to ~1 GB, the Chrome tab
// will crash due to running out of memory.

function randomString(length) {
  var alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  var result = '';
  for (var i = 0; i < length; i++) {
    result +=
        alphabet[Math.round(Math.random() * (alphabet.length - 1))];
  }
  return result;
};

var substrings = [];
var extractSubstring = function(huge_string) {
  var substring = huge_string.substr(0, 100 * 1000 /* 100 KB */);
  // Uncommenting this line will force a copy of the string and allow
  // the unused memory to be garbage collected
  // substring = (' ' + substring).slice(1);
  substrings.push(substring);
};

// Process 100 MB of data, but only keep 1 MB.
for (var i =  0; i < 10; i++) {
  console.log(10 * (i + 1) + 'MB processed');
  var huge_string = randomString(10 * 1000 * 1000 /* 10 MB */);
  extractSubstring(huge_string);
}

// Do something which will keep a reference to substrings around and
// prevent it from being garbage collected.
setInterval(function() {
  var i = Math.round(Math.random() * (substrings.length - 1));
  document.body.innerHTML = substrings[i].substr(0, 10);
}, 2000);

2 of 11
54

not sure how to test, but does using string interpolation to create a new string variable work?

newString = `${oldString}`
🌐
Rosetta Code
rosettacode.org › wiki › Copy_a_string
Copy a string - Rosetta Code
4 weeks ago - Instead, maybe you want to create a StringBuffer (mutable string) from an existing String or StringBuffer: ... Objects can be copied in JavaScript via simple reassignment.
Discussions

javascript - Does passing a string to a function copy it by value or pass it by reference? - Stack Overflow
Also since strings are immutable ... need to copy them since they can't change. ... "The string will be passed by reference." You state that as fact. Do you have any evidence for it? I believe it's true (or more accurately, I believe a reference to the string is passed by value, nothing in JavaScript is "pass by ... More on stackoverflow.com
🌐 stackoverflow.com
How to copy a string into an array of chars without using any built-in function?
If you already have an array of strings, a string being an array of char that ends with "\0", you can just iterate over a[0] and it will iterate over "Hello" ? Now if you need to copy it, just create another array with malloc and copy every char by iterating over a[0] More on reddit.com
🌐 r/C_Programming
14
0
December 9, 2023
Can I copy a hard-coded string to the clipboard with JS?
See: https://developer.mozilla.org/en-US/docs/Web/API/Clipboard More on reddit.com
🌐 r/learnjavascript
3
1
April 21, 2021
How can I copy a string to clipboard on a client easily?
Literally the first hit on google: https://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript Like, you didn't even fucking google it. More on reddit.com
🌐 r/rails
8
0
August 15, 2017
🌐
Reactgo
reactgo.com › home › how to make copy of a string in javascript
How to make copy of a string in JavaScript | Reactgo
December 15, 2022 - Similary, you can also do it by assigning the old string to a new variable. const str = "apple"; const copy = str; console.log(copy); // "apple ... How to remove first element of a array in JavaScriptHow implement merge sort algorithm in JavaScriptHow to sort an array of numbers in JavaScriptHow to split the strings in JavaScriptHow to modify a URL without reloading the page in JavaScriptType checking in JavaScript using typeof operator
🌐
W3Schools
w3schools.com › c › ref_string_strcpy.php
C string strcpy() Function
Note: Make sure that the destination string has enough space for the data or it may start writing into memory that belongs to other variables. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
Medium
marian-caikovski.medium.com › what-is-copied-when-a-string-is-assigned-the-string-or-the-reference-73ac213c1466
Strings are not copied in JavaScript | by Marian C. | Medium
October 16, 2021 - There are plenty of methods returning new strings but none can modify an existing string. For example, you cannot change a character in a string without creating a new string. Copying potentially huge values that cannot change would be a pointless waste of computer resources.
🌐
Delft Stack
delftstack.com › home › howto › javascript › javascript copy string
How to Force JavaScript to Deep Copy a String | Delft Stack
February 2, 2024 - When you copy a string, its address is stored in the specified String variable in JavaScript. Let’s look at this code segment to understand the concept better. let original_string = 'Hello'; var string_copy = (' ' + original_string).slice(1); In this code segment, as you can see, there was a variable named original_string, which had a string value Hello.
Find elsewhere
🌐
DEV Community
dev.to › david_bilsonn › how-to-save-a-string-to-clipboard-in-javascript-13ie
How to save a string to clipboard in JavaScript - DEV Community
September 10, 2024 - The string stored in the stringText variable will be copied to clipboard, you can then paste it within the textarea. Okay, now you are thinking "Can't I make everything happen within HTML?"... Of course you can. How? You can create a paragraph and store its textContent as variable. The copyBtn function can then fetch the variable and store the textContent of the paragraph in the clipBoard. Go ahead and give it a try. Make sure your JavaScript ...
🌐
CodeSweetly
codesweetly.com › javascript-string-slice-method
slice() in JavaScript – How to Clone String Elements | CodeSweetly
JavaScript's slice() method duplicates a specified part of its calling string into a new string—without altering the original string.
🌐
W3Schools
w3schools.com › c › ref_string_strncpy.php
C string strncpy() Function
The strncpy() function copies the first n characters from one string into the memory of another string.
🌐
Java2s
java2s.com › ref › javascript › javascript-string-copy.html
Javascript String copy()
String.prototype.copy = function() { return this.substring( 0, this.length ); }; PreviousNext · Javascript String contem(subtexto, ignoreCase = false) Javascript String convertHTML(str) Javascript String convertUnderscores() Javascript String count() Javascript String count(c)
🌐
Medium
medium.com › @konsumer › c-strings-and-javascript-b79784bc921e
C strings and javascript. Often, when doing javascript stuff, it… | by David Konsumer | Medium
August 21, 2023 - Essentially, this will take a string, and return a string with a greeting, like hello("David") => “Hello David". The other C-stuff is just “take a string-pointer, add it to another string, from this address, then put it into this other memory address.”
🌐
Medium
medium.com › copy-best-practices-javascript-and-typescript › copy-best-practices-javascript-and-typescript-4ff8b9905af4
Copy — Best practices — JavaScript and TypeScript
May 11, 2025 - Changing the property of a nested copied object ✅ YUP · Adding a new property for a nested copied object ✅ YUP · // JavaScript let objectSamples = { name: "John", details: { age: 30 } }; let obj_copy = JSON.parse(JSON.stringify(objectSamples)); obj_copy.name = "Natalia"; obj_copy.surname = 'Nowak'; obj_copy.details.age = 56; obj_copy.details.nationality = 'Island'; console.log(objectSamples.name); // "John" console.log(obj_copy.name); // "Natalia" console.log(objectSamples.surname); // error TS2339: Property 'surname' does not exist on type '{ name: string; details: { age: number; }; }'.
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-basic-exercise-57.php
JavaScript basic: Create a new string of specified copies of a given string - w3resource
July 1, 2025 - This JavaScript program takes a string and a positive number as inputs, then creates a new string by repeating the given string the specified number of times. The result is a single string made up of the original string copied multiple times.
🌐
W3Schools
w3schools.com › cpp › ref_cstring_strcpy.asp
C++ cstring strcpy() Function
Note: Make sure that the destination string has enough space for the data or it may start writing into memory that belongs to other variables. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › reference-and-copy-variables-in-javascript
Reference and Copy Variables in JavaScript - GeeksforGeeks
While JavaScript is often described ... This distinction becomes especially important when copying or modifying data. JavaScript passes primitive data types (Boolean, Null, Undefined, String, Number) by value, meaning changes do not affect the original data....
Published   January 22, 2026