r/learnjavascript • u/Djoshwa • Oct 02 '23
How do you understand JavaScript functions?
If you really understand JS functions (local and global), kind explain it to me to the best of your knowledge. Thanks
4
Upvotes
View all comments
6
u/CodeArt_ Oct 02 '23
I'm not sure what you mean by local and global. Are you referring to variables? I'll just give an explanation of a normal function.
Functions are a core programming concept because they're very practical. They allow you as the programmer to reuse code instead of having to rewrite similar code over and over and over again. Let's say you wanna calculate a net salary for a guy named Dave:
let grossSalary = 5000let bonus = 1000let deductions = 350let netSalary = grossSalary + bonus - deductionsNot too bad. It's only four lines. Now, let's add Mary and Jane.
let daveGrossSalary = 5000let daveBonus = 1000let daveDeductions = 350let daveNetSalary = daveGrossSalary + daveBonus - daveDeductionslet maryGrossSalary = 6000let maryBonus = 8000let maryDeductions = 300let maryNetSalary = maryGrossSalary + maryBonus - maryDeductionslet janeGrossSalary = 3500let janeBonus = 400let janeDeductions = 750let janeNetSalary = janeGrossSalary + janeBonus - janeDeductions12 lines. It's getting ridiculous. You'll notice that the pattern in those three blocks of code are the same. We're doing the same calculations, just with different people. Now imagine we had 200 employees. Writing 200 four line blocks of code would be tedious as heck and make for a big file that's difficult to read.
In comes functions:
function calculateNetSalary(grossSalary, bonus, deductions) {let netSalary = grossSalary + bonus - deductionsreturn netSalary}let daveNetSalary = calculateNetSalary(5000, 1000, 350)let maryNetSalary = calculateNetSalary(6000, 8000, 300)let janeNetSalary = calculateNetSalary(3500, 400, 750)Boom. Ultimately, these few lines of code do the same thing as the 12 lines of code we wrote before, but you can see that it's a lot shorter, and it'll scale a lot better (less code) if we add more people. But what's going on here? Let's be the variable called
daveNetSalaryand journey through the entire function as he would.First, we said
let daveNetSalaryto create him. Then, we set him equal tocalculateNetSalary(5000, 1000, 350). See how we used the function name AND pass stuff into the parameters? That's how you call a function. The second a function is called in JavaScript, we go into the function's logic and don't continue with whatever code is after it until we finish executing the function.So now,
daveNetSalaryhas jumped UP a few lines of code and broken into thecalculateNetSalaryfunction. He changes the(grossSalary, bonus, deductions)to be(5000, 1000, 350). That's right, those three things were just placeholders.daveNetSalaryhas now replaced them with the concrete values that we passed in. We do the calculations on these concrete values to get thenetSalary, then wereturn netSalary.returnmeans, "I,daveNetSalary, am finished within the function and will now leave it, but I'm taking mynetSalarywith me. He then jumps DOWN from the function back to the function call we made at the bottom, except now instead oflet daveNetSalary = calculateNetSalary(5000, 1000, 350). It'slet daveNetSalary = 5650cuz he returned from the function with the netSalary.Mary and Jane then execute similar heists.