Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, in C "A string is an array of characters with last elem = '\0' ". They are mutable.<br> If you declare and initialize a string in C like this:</p> <pre><code>char str[] = "Foo"; </code></pre> <p>What you are basically doing is reserving 4 bytes ( probably 8bit-byte, don't mind this probably if it hurts you ). The word str serves as a pointer to the first elem of this array. So, if you do like this: </p> <pre><code>str[0] or *(str) = 'G' </code></pre> <p>then it will mutate the value at that address instead of creating new array. You can verify it by printing out the address of str. In both cases it will be same.</p> <p>Now in case of JavaScript string is a primitive type. All operations on string are done by value instead of by reference. So, doing this will produce true.</p> <pre><code>var str1 = "foo"; var str2 = "foo"; str1 === str2; =&gt; true </code></pre> <p>The initialization of string asks for a buffer to fit "foo" and binds the name str1 to it. What makes them immutable is that you can't change that buffer. So, you can't do this:</p> <pre><code>str1[0] = 'G' </code></pre> <p>Executing this command will produce no warning or error in non-strict mode but, it will not change the str1. You can verify it by </p> <pre><code>console.log(str1) =&gt; "foo" </code></pre> <p>But if you do like this: </p> <pre><code>str1 = "goo" </code></pre> <p>what you are actually doing is that you are asking for a new buffer to fit "goo" and bind identifier str1 to it. No change in that old buffer containing "foo".</p> <p>So, what happens to "foo"?</p> <p>Java Script has an automatic garbage collector. When it sees some chunk of memory that no longer can be referenced by any identifier or ... then it consider that memory free.</p> <p>Same happens to number,booleans. Now, about wrapper objects! Whenever you try to access a property on string like this:</p> <pre><code>str1.length; </code></pre> <p>What JavaScript does it creates a new object using String class and invoke the methods on string. As soon as the function call returns, the object is destroyed. The below code explains it further:</p> <pre><code>var str = "nature"; str.does = "nurtures"; //defining a new property; console.log(str.does) =&gt; undefined </code></pre> <p>because the object has been destroyed. Try this!</p> <pre><code>var str = new String("Nature"); str.does = "nurtures"; console.log(str) =&gt; ?? </code></pre> <p>this str is really an object...</p> <p>Conclusion: In C , in a single scope the variable name serves as a pointer. So, int, float, string all are mutable. But in Java Script a primitive type variable name serves as value not as reference</p> <p>References: C++ primer plus, Java Script The Definitive Guide, C by Stephen Kochan</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload