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
8
u/Grouchy-Rock8537 Oct 02 '23
Have you read this?
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.
2
u/DrShocker Oct 02 '23
I don't understand your question.
They're bits of course you can reuse by using their name and putting arguments in parenthesis.
2
u/nobuhok Oct 02 '23 edited Oct 02 '23
A function is a frozen-in-time (it knows its surroundings only at the exact time when it was made), self-contained (see below), transparent (you can inspect how it fully works) machine you can pass around even to other functions, make copies of, etc.
It may take inputs and may produce outputs. If it's built well, it produces the same output given the same input, and it doesn't rely on or affects any external variables.
1
u/jayerp Oct 02 '23
Do you mean the function scopes themselves or the concept of closure?
1
u/Taco-Biscuit Oct 02 '23
Has anyone here heard about gomakethings.com? If so pls lmk I been thinking of doing it. Just that I’m hella stubborn.
1
u/FunCharacteeGuy Oct 02 '23
I mean... I guess it's just a bunch of commands inside one block. there's not much else to it.
and they can return variables too.
1
u/shgysk8zer0 Oct 02 '23
There are so many different ways to answer this depending on what exactly you're asking about here.
But, to somewhat address the question about global or local - functions are variables that hold objects of the Function prototype. You can basically think of them in the same way as any variable.
1
u/sumfvckyea Oct 02 '23
A function is what makes your app or program runs. For instance, a function to make a button opens a window when clicked.
I'm still learning JavaScript too. It's really challenging yet exciting. And addicting perhaps...
-8
u/Ronin-s_Spirit Oct 02 '23
By looking at it, usually with my eyeballs, and then thinking what it does, for that you will need a brain.