From the course: JavaScript: Best Practices for Functions and Classes

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Leave parameter values untouched

Leave parameter values untouched - JavaScript Tutorial

From the course: JavaScript: Best Practices for Functions and Classes

Start my 1-month free trial

Leave parameter values untouched

- [Instructor] Within a function, parameters can be almost indistinguishable from variables. They not only store values, but in theory you can even change those values. The start file for this video contains a function that takes a parameter, and then updates its value based on the result of a conditional construction. When I run it, I can see the results from my three test cases. Now, every case results in the value of input being increased. So I'd also like to understand how much my input increases each time. But because I've changed the value of my parameter, I no longer have access to it to compare to another value. There are some other more nuanced reasons to avoid changing the value of a parameter, or even reassigning it if it's an object. But for me, it's as simple as wanting to keep the original value distinct from whatever I do to it in my function. And here, that will let me calculate the difference at the end…

Contents