Avoid Side Effects (part 2) - Uses confusing pass-by-reference terminology
See original GitHub issuePass by reference is an older concept that doesn’t truly exist in most modern programming languages. Because of this, it’s meaning has become confused and twisted. It’s also generally discouraged where it does exist. I recommend using different terms to avoid this confusion.
To understand true pass by reference, please look at this example code from the PHP docs here: https://www.php.net/manual/en/language.references.pass.php.
<?php
function foo(&$var)
{
$var++;
}
$a=5;
foo($a);
// $a is 6 here
?>
Note how the how the original variable $a
has it’s value reassigned to be 6 because $var
becomes an alias to that variable - it’s like they’re the same variable.
This is not possible in JavaScript.
Even with objects and arrays in JavaScript, you can’t assign a new object to a variable outside a function by assigning the new object to a parameter of that function. (You can only change the original object using the parameter.)
Because of this, I recommend using the concept of Mutability to convey the information about how objects and arrays can be changed when passed to a function. Null, undefined, booleans, numbers, and strings are all immutable values and we don’t need to worry about a function changing them. Objects and arrays are mutable values and thus we need to take care that we don’t unintentionally change their contents/properties inside a function.
I want to credit Dan Abramov and his Just JavaScript course with introducing me to this idea. I’ve used JS for years and have only recently solidified my understanding of pass by value and this newer way of thinking about it recently thanks to him.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:5 (3 by maintainers)
Or alternatively, I think the sentence about pass by reference could simply be removed and the section would still make sense.
I’d be happy to submit a PR for either option.
This isn’t the best place to ask about learning/career advice, but I know it can be hard to find sometimes so here’s a quick answer:
freeCodeCamp
That’s really the only advice I think you need. Go through the certification programs in order - they’re completely FREE - and do the projects completely. This is key - make real things and don’t start the next thing until you’ve got the current one working and in a finished state (something you’d be happy to show off in an interview or to your family and friends).
I would also recommend not skipping any of the challenges or projects. If you’ve already learned the material, you can skip the intros and just do the steps to get the tests to pass. (It’ll either go really quickly or you’ll realize you didn’t understand it as well as you thought and can go back and review the information.) They also have a great forum if you need help on the projects.