Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

just a pedantic detail, strings are passed in javascript by reference, they are just immutable


I just went down the rabbit hole of reading this post and the entire thread. As someone who has been looking for a junior job, it's probably the most depressing thing I've ever read. I've been on the market for over 6 months, I've sent countless resumes out and tried various techniques, but I'm not even getting a nibble.


I guess technically it's passed the reference to the string right, so if I say a = "stringA" there is a reference to "stringA" and that is assigned to a if I then say a = "stringAA" there is another reference created for "stringAA" and assigned to a, while "stringA" is sitting around somewhere waiting to be garbage collected in a few milliseconds - that's way complicated to think about and not sure if I haven't messed it up.

Easier to just say pass by value and forget about it. OR make all your variables consts and then it don't matter.


No, thats not correct. Value and reference assignment behave the same way for = (well, reference is hiding the fact that it’s not the literal string/object but a reference to it, a number is just the number).

Where it matters is in passing arguments to a function call. If you pass 42, it’s not mutable so incrementing, or doing anything, will not modify the original variable you passed-in. For a reference, using = will assign a new value (not change the original) but modifying the referenced object like, say a.b = 5 WILL change the original object.

It’s not really “pass by reference” that a C/C++ developer would understand but it seems to be the term that has stuck.


>= (well, reference is hiding the fact that it’s not the literal string/object but a reference to it, a number is just the number).

>For a reference, using = will assign a new value (not change the original)

what I wrote was regarding only strings, so I'm not understanding - it seems you are saying the same thing I said? But maybe I'm wrong about how the actual strings are stored.


Sorry to get a bit nerdy here, but in JS, neither pass by value nor pass by reference make sense as it’s not defined by the spec and much less followed by the implementations. Strings can be pointers to buffers or ropes, numbers can be values (NaN-boxed or otherwise) or pointers depending on a number of conditions, it all depends. However, from what’s observable in the language, all variables are pass by value. There’s no way to pass anything at all by reference, primitive or not, i.e. you can modify a field of an object you were passed but you can’t change the object.


Hashtable of all strings made in program.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: