简体   繁体   中英

Strings: Primitive type variables or reference type variables in javascript?

I was reading in my javascript book again today and it was explaining the difference between reference type variables and primitive type variables. It gave these examples to illustrate the difference.

Example 1 (Primitive Type)

var a = 3.14;
var b = a;
a = 4;
alert( b ); // Displays 3.14

Example 2 (Reference Type)

var a = [1, 2, 3];
var b = a;
var a[0] = 99;
alert( b ); // Displays the changed array [99, 2, 3]

I understand this example and I don't have any questions about it. My question is about strings in javascript. Intuitively, I would assume that strings are reference type variables because if the dynamic size, but I was messing around with them on example.com and I created this example which seems to indicate that strings are primitive type variables.

Example 3 (Strings?)

var a = 'Ben';
var b = a;
var a = 'Benjamin';
alert( b ); //Displays the unchanged 'Ben'

I've searched around here on stack overflow and on google and I found a few articles that talked about this but most of them were talking about other languages such as Java and C#.

Question: In javascript are Strings considered primitive or reference variable types and are there any other situations that I should be aware of where Strings work differently then I would expect?

When you do a = 'Benjamin' , the assignment operator ( = ) changes the object to which a points. It does not change to what object other variables point. To illustrate:

var a, b;

a = "Ben";      // `a` now points to "Ben" in memory
b = a;          // `b` now points to "Ben" in memory
a = "Benjamin"; // `a` now points to "Benjamin" in memory, but
                // `b` still points to "Ben" 

This isn't anything special to do with strings; the semantics are the same for any primitive or object value.

An important part of the answer to your question is, as Bergi points out, that strings in JavaScript are immutable, and so regardless of implementation they act, essentially, as primitives. Again, to illustrate:

a = "Ben";    // `a` now points to "Ben" in memory
b = a;        // `b` now points to "Ben" in memory
a += "jamin"; // `a` now points to "Benjamin" in memory, but
              // `b` still points to "Ben"

a += "jamin" , which as you know is equivalent to a = a + "jamin" , doesn't change the string that a and b point to; it creates a new string with the value "Benjamin" and makes a point to it instead of "Ben" . (Whether or not "Ben" and "Benjamin" share any of the same bytes in memory is an implementation detail that is completely opaque to the JavaScript program.)

No. In JavaScript, strings are immutable , and therefore a primitive type.

That they have variable sizes is just like numbers of different magnitude. Their size is not dynamic , you cannot change the size of an existing string value. Whenever you do string operations, you are creating new values. Just like var x = 1; x += 2 var x = 1; x += 2 won't change the 1 value (but create a new 3 value), var var a = "Hi!"; a += "!" var var a = "Hi!"; a += "!" won't change the string "Hi!" .

In JavaScript strings are primitives.

There is a thing you should be avare of, though. Wraper objects. primitives like strings, numbers, booleans get wrapped up in objects, so you can call properties on these wrapper objects, for insance:

var foo = 'bar';
var poo = foo.slice(0,-1)
poo == 'ar'; //true

Here string 'bar' is wrapped up in object that has slice method ( and many others ), but in all other cases ( when you just pass primitive variables around, etc. ) the variable is just a primitive.

Example 3 doesn't work because, as Jordan said, if you change the value of an actual object and not just a property, the objects that object point to won't change even though object is a reference type in JavaScript.

However, strings are immutable and primitive values, so even if we did something like this:

var a = "Hi!";
var b = a;
a[2] = "?";
alert(a);
alert(b);

a would still be "Hi!" because strings are immutable and b would still be "Hi!" even if a was "Hi?" because strings are primitive values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM