DEV Community

Mehul Lakhanpal
Mehul Lakhanpal

Posted on

Arrays and objects are stored by reference

Alt Text

Thanks for reading 💙

Follow @codedrops.tech for daily posts.

InstagramFacebook

Micro Learning ● Web Development ● Javascript ● MERN stack

www.codedrops.tech

Top comments (18)

Collapse
 
aleksandrhovhannisyan profile image
Aleksandr Hovhannisyan

This wording may confuse beginners because there's really no general programming concept known as "storing by reference." There are things called references, and passing by reference, but not storing by reference.

A more accurate statement: JavaScript has object references. These are passed by value.

Collapse
 
pentacular profile image
pentacular

I haven't been able to find "object references" in the language specification so far.

Could you let me know where to look?

Collapse
 
ml318097 profile image
Mehul Lakhanpal

What is it then? And btw, this is how someone would explain to a beginner. I feel @aleksandr has explained it nicely.

Thread Thread
 
pentacular profile image
pentacular

If you feel it has been explained nicely, why are you asking what it is? :)

But I'm also wondering -- what is an object reference (which I can't find in the language specification) and how does it differ from an object value (which I can find in the language specification)?

Thread Thread
 
ml318097 profile image
Mehul Lakhanpal

Because I want to know what is a better way. Everyone is not into specs. Some idea is required to understand the concept. There is abstraction in every level of knowledge. Probably you can read anything other than the spec and you can find the answers 😀🤘

Thread Thread
 
pentacular profile image
pentacular

What I've understood is that object values are just like primitive values, the difference is that there are properties you can look up given an object value.

Which seems very simple, and avoids adding references, and the mystery of what they reference, and avoids confusion with pass by reference, or having a reference that you pass by value, but that value isn't a reference or the thing being referred to ... so, what is that "reference value" supposed to be?

Thread Thread
 
ml318097 profile image
Mehul Lakhanpal

Ok if they are the same as othere then why are the array not equal? And by reference I mean the address.

Thread Thread
 
pentacular profile image
pentacular

Every distinct array has a unique value.

So [] === [] creates two distinct arrays and compares their values (not their elements or properties).

What is an address in javascript?

Thread Thread
 
aleksandrhovhannisyan profile image
Aleksandr Hovhannisyan

You're being pedantic and it's honestly really off-putting. I have to agree with Manolo Edge—do you not have anything better to do?

Yes, JavaScript doesn't have "references." But it's the accepted terminology in JavaScript land, just like it is in Java and Python.

The important thing to understand is what references are under the hood (pointers). What you call them, at the end of the day, is not important, as long as everyone's on the same page.

Thread Thread
 
pentacular profile image
pentacular

What do they point at?

Thread Thread
 
Sloan, the sloth mascot
Comment deleted
 
pentacular profile image
pentacular

What's a memory address in javascript?

Thread Thread
 
ml318097 profile image
Mehul Lakhanpal • Edited

return && break && throw new Errror('✋');

Thread Thread
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
ml318097 profile image
Mehul Lakhanpal

But 'Javascript has object references' also sounds confusing to me. And yes, pass by value would be better.

Collapse
 
pentacular profile image
pentacular

I had a look in the ecmascript standard, but couldn't find anything about object or arrays being stored by reference.

Could you let me know what I missed?

ecma-international.org/publication...

What I did see was 'object values', which seem sufficient to explain how it all works, given that object properties are separate to object values.

Collapse
 
nombrekeff profile image
Keff

Man, don't you have anything better to do?

I've seen you all around arguing about this... we understand. You are smarter cause you read the whole ECMAScript standard :)

Collapse
 
pentacular profile image
pentacular

So, it isn't part of the language?

Some comments may only be visible to logged-in visitors. Sign in to view all comments.