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 - JavaScript Tutorial
From the course: JavaScript: Best Practices for Functions and Classes
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
Use function expressions instead of declarations6m 20s
-
Don't use the new keyword to construct functions2m 39s
-
Leave parameter values untouched3m 2s
-
Use arrow syntax for anonymous functions1m 36s
-
Rely on implicit return only when obvious3m 26s
-
Challenge: Create functions53s
-
Solution: Create functions5m 29s
-
-
-
-
-
-