Understand String Immutability
This commit is contained in:
committed by
SaintPeter
parent
28b1f99deb
commit
95d7c25679
@ -1188,10 +1188,11 @@
|
||||
"id": "56533eb9ac21ba0edf2244ba",
|
||||
"title": "Understand String Immutability",
|
||||
"description": [
|
||||
"In Javascript, strings are <dfn>immutable</dfn>, which means that they cannot be changed or modified once created. For example, this code:",
|
||||
"<code>var myStr = \"Bob\";<br />myStr[0] = \"J\";</code>",
|
||||
"will not change the contents of <code>myStr</code> to \"Job\", because the contents of myStr cannot be altered. Note that this does <em>not</em> mean that <code>myStr</code> cannot be change, just that individual characters cannot be changes. The only way to change <code>myStr</code> would be to overwrite the contents with a new string, like this:",
|
||||
"<code>var myStr = \"Bob\";<br />myStr = \"Job\";</code>",
|
||||
"In Javascript, <code>String</code> values are <dfn>immutable</dfn>, which means that they cannot be altered once created.",
|
||||
"For example, the following code:",
|
||||
"<blockquote><code>var myStr = \"Bob\";<br>myStr[0] = \"J\";</code></blockquote>",
|
||||
"cannot change the value of <code>myStr</code> to \"Job\", because the contents of myStr cannot be altered. Note that this does <em>not</em> mean that <code>myStr</code> cannot be changed, just that the individual characters of a <dfn>string literal</dfn> cannot be changed. The only way to change <code>myStr</code> would be to assign it with a new string, like this:",
|
||||
"<blockquote><code>var myStr = \"Bob\";<br>myStr = \"Job\";</code></blockquote>",
|
||||
"<h4>Instructions</h4>",
|
||||
"Correct the assignment to <code>myStr</code> to achieve the desired effect."
|
||||
],
|
||||
|
Reference in New Issue
Block a user