Variable and memory

Hello guys, I have just started learning about programming for about a week or so and I did the HTML section in the curriculum (most of it) and have been doing a fair bit of the Java Script curriculum.

I did HTML as a primer before moving on to my objective: learning to program in JavaScript as a hobby.

I’ve been told that variables store primitives but when it comes to object, they point to somewhere in the memory. I don’t understand the concept of the memory reference. Could someone explain?

Keep in mind that I didn’t know what Java Script was 7 days ago so anything but the most simple explanations risk being not understood.

So that’s a little misleading: every variable in a programming language, whether C or javascript, is somewhere in memory. If I have a string, it’s a string stored somewhere in the computer’s RAM (specifically, in that bit of RAM that’s been allocated to the web browser). If I have a boolean, it’s in that same block of memory space. An array, or a number, or an object… they only exist in the browser’s allocated RAM.

An in-depth article on just this subject can be found at SessionStack. Be warned, it’s the DEEP end of the pool.

That said, there is a difference between strings and objects. When I pass them into a function, they are handled very differently: a string is passed in by value, which means the value itself is passed, and not the memory place in which that original variable was stored. In the case of an object (or an array), we don’t pass that to a function by value, we pass it by reference. This means that we are sending our function that memory location and saying “Go ahead and muck with the original copy!”

There are ways around that, as you’ll see when you get to that point, but I think that may be what you are seeing - when passed into a function, primitives (like Numbers or Strings or Booleans) are passed by value, while Objects or Arrays are passed by reference.

2 Likes

Maybe this might make sense: a primitive value, like a number, is kind of a concrete thing. When you look up the variable it’s assigned to, you get that value straight back.

But in JS an object is a collection of values – it’s more of an abstract thing. It can contain many other primitive values. It’s like a bag of variables that all have values assigned to them. So when you look up the variable a particular object is assigned to, what you get back is a thing that lets you access the values in that bag.

This has some practical implications re how you code in JS. The obvious thing is that these are not the same:

const exampleObj1 = { foo: 1 };
const exampleObj2 = { foo: 1 };

Because each one is a different object – the fact they look the same isn’t really relevant. You can add or remove things from those objects, and they will still be the same object.

To continue the metaphor, the variable they are assigned to just points to the bag itself. You can take things out of or put things into that bag as much as you want, but it’s still the same bag.

2 Likes

Thank you for both of your replies.

The bag metaphor does help in understanding that concept. It’s also good to read that it’s not just ‘memory’ but part of the RAM memory.

It’s still very fuzzy in my head, it’s not as clear as I’d like it to be but it’s definitely less obscure.