What are primitive data types? (string immutability)


#1

The instructions for this exercise read:

The very basic building block of JavaScript are primitive data types. We know of three primitives:
* strings (e.g. "dogs go woof!")
* numbers (e.g. 4, 10)
* booleans (e.g. false, 5 > 4)

From the Mozilla JS data type doc I gather that "primitive data types" are one of six things (boolean, null, undefined, number, string, and symbol) which are "immutable", or can't be changed. These are in contrast to objects, which are not primitive because they can be changed. Would this be a correct understanding?

I can understand how this applies to most (don't know yet what the newest, "symbol", is), including numbers, sort of, but string is the most confusing to me, so I'm going to focus on that one. What does it mean that strings are immutable?

I've tried experimenting with it on my own (see below) and reading (ahem, trying to read) this stackexchange, but I'm not confident I understand what it means.

So for example, a variable can point to a string, and if I "change" that string (as per below for example), all I'm really changing is what string the variable is pointing to, and not the original string itself (which is presumably floating around somewhere in the ether out there)?

And if so, is every possible immutable string theoretically "in" every program to begin with, or are programs stringless until I start creating them?

Thanks everyone,
frisby

https://www.codecademy.com/en/courses/spencer-sandbox/0/1?curriculum_id=506324b3a7dffd00020bf661


var test = "hello "
var test2 = "you"
var test3 = test

console.log(test += test2);
console.log(test3);


#2

strings are immutable, you can't change them. If you alter a string, a new string is created, with the changes you requested

Reading a java question on stackexchange, while programming in javascript can indeed be very confusing. Java is to javascript, what ham is to hamster


#3

:flushed: Oh noooooo that is too embarrassing! I'd typed "javascript" when I googled it so I guess I saw "JS" where it actually said "Java"...!!! :scream:

Thank you for your answer, @stetim94. What happens to the old string? If I keep reassigning new strings to the same variable, will the old strings take up increasing amounts of, um, memory(?)? Or does it not work that way...?

PS: gotta love that hamster analogy! :joy:


#4

I believe the old string disappears completely.

If you have a variable with the value "Hello", then reassign the variable to "Hello, world", instead of adding ', world' to 'Hello', it's getting rid of 'Hello', and just replacing it with 'Hello, world'.


#5

i agree with jibblyj here, the old string will disappear, but difficult to say exactly how this happening under the hood. String manipulation is different from assigning a new string to a variable.

The drawback of a higher level language, memory management is done for you (which is also a huge advantage)


Didn't declare a variable part II
#6

Thank you both @jibblyj and @stetim94! I see now; so basically if I assign a new string to a variable the old one is terminated and doesn't continue to exist as some kind of ghost string.

Is it then correct to assume that if I create strings but don't assign them to anything once they are used as intended (e.g. console log), they disappear also? e.g. are the below two distinct strings?

    console.log("I'll be back.")
    console.log("I'll be back.")

#7

yea, they will also just disappear.