From the course: JavaScript: Best Practices for Data
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Don't assign variables in chains - JavaScript Tutorial
From the course: JavaScript: Best Practices for Data
Don't assign variables in chains
- [Instructor] The code for this video does some basic calculations on a set of items in a shopping cart. It starts off declaring variables, and then in line nine, ensures that the subtotal, shipping, and net total variables are all numbers and have a value of zero, by using a chained assignment. This single statement is certainly concise. But like other statements that perform multiple actions, it can be hard to read it and understand exactly what's going on. For this reason, I prefer to always assign my values with individual statements. This makes each line of code more easily digestible when I read it, and does away with corner cases where chained assignment can create uncertainty. ESLint has a rule that can flag this for me, which is no-multi-assign. I'll start by adding that to my ESLint RC file. So it's no dash multi dash assign, the value of error. And then, saving and switching back, and that error is…
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.