Hacker News new | past | comments | ask | show | jobs | submit login

fun fact, I once interviewed at a place in which the tech lead interviewing me had confused the terms pass by reference and pass by value - that is to say he understood that in JavaScript objects were passed by reference and what that meant on the practical effects of assigning object a to b, but he thought the technical term for this was pass by value and the technical term for things that were passed by value was pass by reference (so according to him strings were passed by reference and objects were passed by value) and no explanation on what a reference was and how pass by reference works and why it made sense to call it pass by reference could penetrate.



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.


So the naming is super confusing in these cases and the best way to get out of it is say "the references are passed by value", but... technically he was right. In JS everything's passed by value. It doesn't matter that those values are references. Pass by ref would mean that "function foo(a) {a='123'}; b=''; foo(b)" would change the value of `b`.

Every popular language which allows pass-by-reference makes those places very explicit (like ref in c++ and c#)


>but... technically he was right. In JS everything's passed by value. It doesn't matter that those values are references.

yes, technically I know this but even so he was technically not right because he still said there was pass by reference and pass by value in JavaScript, it's just that the description he had of what happens in pass by value is what is normally described as "pass by reference" and the description he had of what happens in pass by reference is what is normally described as "pass by value".

I think we can agree that given that he used both terms and mixed up their meanings that he was not "technically right"

on edit: meaning if he had said "we pass everything by value in JS but some values are references, what happens then?" he would be right, but when he said we pass objects by value and primitives by reference - what do these two terms mean and then he accepted the description of what happens in an object when passing the reference as being correct but insisted that was called pass by value, and he accepted that the description of what happened with a variable when it has a string assigned and then that variable is assigned to another variable and then the first variable is changed was correct including the ability to change the value of variable A and not have the value of variable B changed thereby but insisted that this process is called pass by reference, I intuited through this conversation that he was unfortunately not "technically correct"


Should've been more clear, it was only a response to the objects passed by value part as correct. Yeah, he was obviously confused by other parts.




Consider applying for YC's Summer 2025 batch! Applications are open till May 13

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

Search: