<p>Does javascript use immutable or mutable strings? Do I need a "string builder"?</p>

[{'answer_id': 51188, 'author': 'JC Grubbs', 'author_id': 4541, 'author_profile': 'https://Stackoverflow.com/users/4541', 'pm_score': 0, 'selected': False, 'text': '<p>JavaScript strings are indeed immutable.</p>\n'}, {'answer_id': 51191, 'author': 'Glenn Slaven', 'author_id': 2975, 'author_profile': 'https://Stackoverflow.com/users/2975', 'pm_score': 0, 'selected': False, 'text': '<p>Strings in Javascript are immutable</p>\n'}, {'answer_id': 51193, 'author': 'minty', 'author_id': 4491, 'author_profile': 'https://Stackoverflow.com/users/4491', 'pm_score': 6, 'selected': False, 'text': '<p>from the <a href="http://oreilly.com/catalog/9780596000486/" rel="nofollow noreferrer">rhino book</a>:</p>\n\n<blockquote>\n  <p>In JavaScript, strings are immutable objects, which means that the\n  characters within them may not be changed and that any operations on\n  strings actually create new strings. Strings are assigned by\n  reference, not by value. In general, when an object is assigned by\n  reference, a change made to the object through one reference will be\n  visible through all other references to the object. Because strings\n  cannot be changed, however, you can have multiple references to a\n  string object and not worry that the string value will change without\n  your knowing it</p>\n</blockquote>\n'}, {'answer_id': 51199, 'author': 'Ash', 'author_id': 5023, 'author_profile': 'https://Stackoverflow.com/users/5023', 'pm_score': 5, 'selected': False, 'text': '<p><strong>Performance tip:</strong></p>\n\n<p>If you have to concatenate large strings, put the string parts into an array and use the <code>Array.Join()</code> method to get the overall string.  This can be many times faster for concatenating a large number of strings.  </p>\n\n<p>There is no <code>StringBuilder</code> in JavaScript.</p>\n'}, {'answer_id': 51215, 'author': 'BirgerH', 'author_id': 5164, 'author_profile': 'https://Stackoverflow.com/users/5164', 'pm_score': 1, 'selected': False, 'text': '<p>Regarding your question (in your comment to Ash\'s response) about the StringBuilder in ASP.NET Ajax the experts seem to disagree on this one.</p>\n\n<p>Christian Wenz says in his book <em>Programming ASP.NET AJAX</em> (O\'Reilly) that "this approach does not have any measurable effect on memory (in fact, the implementation seems to be a tick slower than the standard approach)."</p>\n\n<p>On the other hand Gallo et al say in their book <em>ASP.NET AJAX in Action</em> (Manning) that "When the number of strings to concatenate is larger, the string builder becomes an essential object to avoid huge performance drops."</p>\n\n<p>I guess you\'d need to do your own benchmarking and results might differ between browsers, too. However, even if it doesn\'t improve performance it might still be considered "useful" for programmers who are used to coding with StringBuilders in languages like C# or Java.</p>\n'}, {'answer_id': 4717855, 'author': 'Ruan Mendes', 'author_id': 227299, 'author_profile': 'https://Stackoverflow.com/users/227299', 'pm_score': 9, 'selected': True, 'text': '<p>They are immutable. You cannot change a character within a string with something like <code>var myString = &quot;abbdef&quot;; myString[2] = \'c\'</code>. The string manipulation methods such as <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim" rel="noreferrer"><code>trim</code></a>, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice" rel="noreferrer"><code>slice</code></a> return new strings.</p>\n<p>In the same way, if you have two references to the same string, modifying one doesn\'t affect the other</p>\n<pre><code>let a = b = &quot;hello&quot;;\na = a + &quot; world&quot;;\n// b is not affected\n</code></pre>\n<p>However, I\'ve always heard what Ash mentioned in his answer (that using Array.join is faster for concatenation) so I wanted to test out the different methods of concatenating strings and abstracting the fastest way into a StringBuilder. I wrote some tests to see if this is true (it isn\'t!).</p>\n<p>This was what I believed would be the fastest way, though I kept thinking that adding a method call may make it slower...</p>\n<pre><code>function StringBuilder() {\n    this._array = [];\n    this._index = 0;\n}\n\nStringBuilder.prototype.append = function (str) {\n    this._array[this._index] = str;\n    this._index++;\n}\n\nStringBuilder.prototype.toString = function () {\n    return this._array.join(\'\');\n}\n</code></pre>\n<p>Here are performance speed tests. All three of them create a gigantic string made up of concatenating <code>&quot;Hello diggity dog&quot;</code> one hundred thousand times into an empty string.</p>\n<p>I\'ve created three types of tests</p>\n<ul>\n<li>Using <code>Array.push</code> and <code>Array.join</code></li>\n<li>Using Array indexing to avoid <code>Array.push</code>, then using <code>Array.join</code></li>\n<li>Straight string concatenation</li>\n</ul>\n<p>Then I created the same three tests by abstracting them into <code>StringBuilderConcat</code>, <code>StringBuilderArrayPush</code> and <code>StringBuilderArrayIndex</code> <a href="http://jsperf.com/string-concat-without-sringbuilder/5" rel="noreferrer">http://jsperf.com/string-concat-without-sringbuilder/5</a> Please go there and run tests so we can get a nice sample. Note that I fixed a small bug, so the data for the tests got wiped, I will update the table once there\'s enough performance data. Go to <a href="http://jsperf.com/string-concat-without-sringbuilder/5" rel="noreferrer">http://jsperf.com/string-concat-without-sringbuilder/5</a> for the old data table.</p>\n<p>Here are some numbers (Latest update in Ma5rch 2018), if you don\'t want to follow the link. The number on each test is in 1000 operations/second (<strong>higher is better</strong>)</p>\n<div class="s-table-container">\n<table class="s-table">\n<thead>\n<tr>\n<th>Browser</th>\n<th>Index</th>\n<th>Push</th>\n<th>Concat</th>\n<th>SBIndex</th>\n<th>SBPush</th>\n<th>SBConcat</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>Chrome 71.0.3578</td>\n<td>988</td>\n<td>1006</td>\n<td>2902</td>\n<td>963</td>\n<td>1008</td>\n<td>2902</td>\n</tr>\n<tr>\n<td>Firefox 65</td>\n<td>1979</td>\n<td>1902</td>\n<td>2197</td>\n<td>1917</td>\n<td>1873</td>\n<td>1953</td>\n</tr>\n<tr>\n<td>Edge</td>\n<td>593</td>\n<td>373</td>\n<td>952</td>\n<td>361</td>\n<td>415</td>\n<td>444</td>\n</tr>\n<tr>\n<td>Exploder 11</td>\n<td>655</td>\n<td>532</td>\n<td>761</td>\n<td>537</td>\n<td>567</td>\n<td>387</td>\n</tr>\n<tr>\n<td>Opera 58.0.3135</td>\n<td>1135</td>\n<td>1200</td>\n<td>4357</td>\n<td>1137</td>\n<td>1188</td>\n<td>4294</td>\n</tr>\n</tbody>\n</table>\n</div>\n<p><strong>Findings</strong></p>\n<ul>\n<li><p>Nowadays, all evergreen browsers handle string concatenation well. <code>Array.join</code> only  helps IE 11</p>\n</li>\n<li><p>Overall, Opera is fastest, 4 times as fast as Array.join</p>\n</li>\n<li><p>Firefox is second and <code>Array.join</code> is only slightly slower in FF but considerably slower (3x) in Chrome.</p>\n</li>\n<li><p>Chrome is third but string concat is 3 times faster than Array.join</p>\n</li>\n<li><p>Creating a StringBuilder  seems to not affect perfomance too much.</p>\n</li>\n</ul>\n<p>Hope somebody else finds this useful</p>\n<p><strong>Different Test Case</strong></p>\n<p>Since @RoyTinker thought that my test was flawed, I created a new case that doesn\'t create a big string by concatenating the same string, it uses a different character for each iteration. String concatenation still seemed faster or just as fast. Let\'s get those tests running.</p>\n<p>I suggest everybody should keep thinking of other ways to test this, and feel free to add new links to different test cases below.</p>\n<p><a href="http://jsperf.com/string-concat-without-sringbuilder/7" rel="noreferrer">http://jsperf.com/string-concat-without-sringbuilder/7</a></p>\n'}, {'answer_id': 32074982, 'author': 'GibboK', 'author_id': 379008, 'author_profile': 'https://Stackoverflow.com/users/379008', 'pm_score': 2, 'selected': False, 'text': '<p><strong>Strings are immutable</strong> – they cannot change, we can only ever make new strings.</p>\n\n<p>Example:</p>\n\n<pre><code>var str= "Immutable value"; // it is immutable\n\nvar other= statement.slice(2, 10); // new string\n</code></pre>\n'}, {'answer_id': 33127871, 'author': 'zhanziyang', 'author_id': 4315171, 'author_profile': 'https://Stackoverflow.com/users/4315171', 'pm_score': 2, 'selected': False, 'text': '<p>The string type value is immutable, <strong>but</strong> the String object, which is created by using the String() constructor, is mutable, because it is an object and you can add new properties to it.</p>\n\n<pre><code>&gt; var str = new String("test")\nundefined\n&gt; str\n[String: \'test\']\n&gt; str.newProp = "some value"\n\'some value\'\n&gt; str\n{ [String: \'test\'] newProp: \'some value\' }\n</code></pre>\n\n<p>Meanwhile, although you can add new properties, you can\'t change the already existing properties</p>\n\n<p><a href="http://i.stack.imgur.com/m9y8P.png" rel="nofollow">A screenshot of a test in Chrome console</a></p>\n\n<p>In conclusion, \n1. all string type value (primitive type) is immutable. \n2. The String object is mutable, but the string type value (primitive type) it contains is immutable.</p>\n'}, {'answer_id': 51652749, 'author': 'revelt', 'author_id': 3943954, 'author_profile': 'https://Stackoverflow.com/users/3943954', 'pm_score': 1, 'selected': False, 'text': '<p>It\'s a late post, but I didn\'t find a good book quote among the answers.</p>\n\n<p>Here\'s a definite except from a reliable book:</p>\n\n<blockquote>\n  <p>Strings are immutable in ECMAScript, meaning that once they are created, their values cannot change. To change the string held by a variable, the original string must be destroyed and the variable filled with another string containing a new value...\n  <em>—Professional JavaScript for Web Developers, 3rd Ed., p.43</em></p>\n</blockquote>\n\n<p>Now, the answer which quotes Rhino book\'s excerpt is right about string immutability but wrong saying "Strings are assigned by reference, not by value." (probably they originally meant to put the words an opposite way).</p>\n\n<p>The "reference/value" misconception is clarified in the "Professional JavaScript", chapter named "Primitive and Reference values":</p>\n\n<blockquote>\n  <p>The five primitive types...[are]: Undefined, Null, Boolean, Number, and String. These variables are said to be accessed by value, because you are manipulating the actual value stored in the variable.\n  <em>—Professional JavaScript for Web Developers, 3rd Ed., p.85</em></p>\n</blockquote>\n\n<p>that\'s opposed to <em>objects</em>:</p>\n\n<blockquote>\n  <p>When you manipulate an object, you’re really working on a reference to that object rather than the actual object itself. For this reason, such values are said to be accessed by reference.<em>—Professional JavaScript for Web Developers, 3rd Ed., p.85</em></p>\n</blockquote>\n'}, {'answer_id': 52242541, 'author': 'Katinka Hesselink', 'author_id': 8007395, 'author_profile': 'https://Stackoverflow.com/users/8007395', 'pm_score': 5, 'selected': False, 'text': '<p>Just to clarify for simple minds like mine (from <a href="https://developer.mozilla.org/en-US/docs/Glossary/Mutable" rel="noreferrer">MDN</a>):</p>\n<blockquote>\n<p>Immutables are the objects whose state cannot be changed once the object is created.</p>\n<p>String and Numbers are Immutable.</p>\n</blockquote>\n<p>Immutable means that:</p>\n<blockquote>\n<p>You can make a variable name point to a new value, but the previous value is still held in memory. Hence the need for garbage collection.</p>\n<p><code>var immutableString = &quot;Hello&quot;;</code></p>\n<p>// In the above code, a new object with string value is created.</p>\n<p><code>immutableString = immutableString + &quot;World&quot;;</code></p>\n<p>// We are now appending &quot;World&quot; to the existing value.</p>\n</blockquote>\n<p>This looks like we\'re mutating the string \'immutableString\', but we\'re not. Instead:</p>\n<blockquote>\n<p>On appending the &quot;immutableString&quot; with a string value, following events occur:</p>\n<ol>\n<li>Existing value of &quot;immutableString&quot; is retrieved</li>\n<li>&quot;World&quot; is appended to the existing value of &quot;immutableString&quot;</li>\n<li>The resultant value is then allocated to a new block of memory</li>\n<li>&quot;immutableString&quot; object now points to the newly created memory space</li>\n<li>Previously created memory space is now available for garbage collection.</li>\n</ol>\n</blockquote>\n'}]
['https://Stackoverflow.com/questions/51185', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1220/']