DEV Community

Discussion on: Understanding the Difference Between Reference and Value in JavaScript

Collapse
 
miketalbot profile image
Mike Talbot ⭐ • Edited

Agree with above of course. Just want to point out to people coming from a C# world that for instance structs in C# are passed by value. You get a copy of an object with all of its members also copied. It ends up on the stack. class instances are passed by "a copy of a reference" if you like often short-handed to "passed by reference". In Javascript only primitives are passed by copying the pure value, objects are always a copy of a reference.

Collapse
 
val_baca profile image
Valentin Baca • Edited

Correct. structs in several languages behave similarly (C, Go, Crystal). The cost of copying becomes larger as the struct becomes larger, which is where pointers come in. Then references were made as a safer alternative to pointers. (obviously glossing over details here)

By the way, I think you mean instances when you wrote "classes are passed by "a copy of a reference""

In the end it all comes down to knowing how pointers work. So many programmers think that if they're not coding in C or C++ then they can just not learn how pointers work since they're dealing with """references"""

Knowing what is exactly copied is incredibly important in every language.

Thread Thread
 
miketalbot profile image
Mike Talbot ⭐

Ooops... Quite right, instances of classes.