Understand String Immutability
This commit is contained in:
committed by
SaintPeter
parent
28b1f99deb
commit
95d7c25679
@ -1188,10 +1188,11 @@
|
|||||||
"id": "56533eb9ac21ba0edf2244ba",
|
"id": "56533eb9ac21ba0edf2244ba",
|
||||||
"title": "Understand String Immutability",
|
"title": "Understand String Immutability",
|
||||||
"description": [
|
"description": [
|
||||||
"In Javascript, strings are <dfn>immutable</dfn>, which means that they cannot be changed or modified once created. For example, this code:",
|
"In Javascript, <code>String</code> values are <dfn>immutable</dfn>, which means that they cannot be altered once created.",
|
||||||
"<code>var myStr = \"Bob\";<br />myStr[0] = \"J\";</code>",
|
"For example, the following 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:",
|
"<blockquote><code>var myStr = \"Bob\";<br>myStr[0] = \"J\";</code></blockquote>",
|
||||||
"<code>var myStr = \"Bob\";<br />myStr = \"Job\";</code>",
|
"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>",
|
"<h4>Instructions</h4>",
|
||||||
"Correct the assignment to <code>myStr</code> to achieve the desired effect."
|
"Correct the assignment to <code>myStr</code> to achieve the desired effect."
|
||||||
],
|
],
|
||||||
|
Reference in New Issue
Block a user