DEV Community

Cover image for JavaScript Functions: Explain Like I'm Five

JavaScript Functions: Explain Like I'm Five

Sumudu Siriwardana on September 27, 2021

If you are a newbie to programming and JavaScript, you might first find it hard to grasp the concept of functions. When I first learned about fun...
Collapse
 
unionjak profile image
unionjak

If you wrote a book about learning javascript as detailed and well explained as the above...you will have a best seller. Javascript books , especially for beginners need to be explained as you have done. Perfect.

Collapse
 
sumusiriwardana profile image
Sumudu Siriwardana

Thank you so much for your kind words! Comments like this really boost my motivation! ❤️

Collapse
 
jonrandy profile image
Jon Randy 🎖️

Regarding function expressions, you've omitted to mention that you can name functions defined in this manner - and in fact it can be a good idea to do so. If they are not named they will show up as 'anonymous' functions in stack traces. So, it is equally valid to write:

const calcAge2 = function calculateAge(birthyear) {
    return 2021 - birthyear;
}
Enter fullscreen mode Exit fullscreen mode

In this case, stack traces involving the function will show the name calculateAge instead of 'anonymous function'

Collapse
 
sumusiriwardana profile image
Sumudu Siriwardana • Edited

Thank you so much for correcting that, Jon! I will add that part.

Just wanted to show the difference as basics without confusing the newbies, because I was not touching other function types in this article. 😂

Collapse
 
peerreynders profile image
peerreynders

I don't disagree with your premise in the least - I prefer explicitly named functions as well.

However introduced with ES2015:

Collapse
 
firefox700101 profile image
Raymond

Does the Arrow Function way really work in this case? I copied and pasted the code to run and set the birthyear to 2020, it still says 'eligible'.

I noticed there is a 'eligibleToDrive' and also a 'EligibleToDrive'. Only the first letter is different, but never seen where to use 'EligibleToDrive'.

Collapse
 
sumusiriwardana profile image
Sumudu Siriwardana

Hi Raymond, sorry for the confusion and there was an error. And thank you for pointing it out. And I corrected it and added a new code.

Collapse
 
firefox700101 profile image
Raymond

Sumudu. It works now. Thank you for the correction.

Collapse
 
da_crazypaine profile image
✨⚡️CrazyPaine⚡️✨

I just wanted to say thank you so much for writing this article because I have so much better understanding of functions than I did now. Thank you so much for breaking this down.
I had so much trouble in understanding how they worked but this actually clicked for me. Please keep writing these.

Collapse
 
madimalik profile image
Madi

I've actually read quite a few articles about JavaScript functions, but I must say that this one is by far the best at explaining them. Now I feel like I have a much better understanding of functions. Thank u so much

Collapse
 
ribosomatic profile image
Jesus Liñan

Me lo leí todo 😊 buen aporte!! Thanks

Collapse
 
shubhracodes profile image
Shubhra Agarwal

Great read. Loved every word of it💯🧡

Collapse
 
sumusiriwardana profile image
Sumudu Siriwardana

Thank you, Shubhra!

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
sumusiriwardana profile image
Sumudu Siriwardana

OMG! I did that, and it worked! Thank you so much!!!!

Collapse
 
posandu profile image
Posandu • Edited

Nice and Amazing Post. You should also start a newsletter :-D

Collapse
 
robertbush profile image
Robertbrushe
Collapse
 
heidinegrete profile image
Heidi Negrete

I will be sharing with my students, great article!

Collapse
 
sumusiriwardana profile image
Sumudu Siriwardana

Thank you 😊

Collapse
 
mike97x profile image
mike97x

Simply the clearest explainations to understand JS functions basic concepts I ever read , as a beginner I never clearly understund the difference between arguments and parameters..until now !
Thank you for writing this article !

Collapse
 
sumusiriwardana profile image
Sumudu Siriwardana

Thank you so much for taking the time to read and for you feedback!

Collapse
 
kiddcreator profile image
kidd-creator

1 more function check my post 😁😁

Collapse
 
professoru profile image
professorU

This is clearest approach ever,Thank you Sumudu.

Collapse
 
sumusiriwardana profile image
Sumudu Siriwardana

I'm glad you liked it. Thank you!

Collapse
 
miqalarcia profile image
Miq

Excellent explanation! But... (though I I resolved by myself, :))) it's me the only one who don't sees the "elegible to drive" code? I see "Years to retirement" code instead.
Thanks so so so much.

Collapse
 
sumusiriwardana profile image
Sumudu Siriwardana

Hi Miq, I have change the paragraph and code due to some errors. Can't you still see the changes? I cannot see any diference.

Collapse
 
miqalarcia profile image
Miq

Hi Sumudu! Now everything seems correct. Thanks for your work.

Collapse
 
lukeryandev profile image
Luke Ryan

Very good breakdown of functions, bookmarked!

~Could I request your next article to be on state management in React? 🚀

Collapse
 
sumusiriwardana profile image
Sumudu Siriwardana

Thank you! I'm still learning JS and haven't done React yet 😞

Collapse
 
otongjr profile image
otongjr

You have explained it in a very simple way that can be understood ezsily.
Thanks!!

Collapse
 
sumusiriwardana profile image
Sumudu Siriwardana

I'm glad to hear that. Thank you for taking the time to read it.

Collapse
 
karan_grover_113f7c167a29 profile image
Karan Grover

Hey, i need help. I accidently ran a command chmod 777 - r on OS file system. Root User disabled and website got database error. How to fix this? URGENT!!

Collapse
 
devluc profile image
Devluc

Awesome article Sumudu, thanks for putting this together

Collapse
 
sumusiriwardana profile image
Sumudu Siriwardana

Thank you so much for your kind words, Lucian!

Collapse
 
tanzimibthesam profile image
Tanzim Ibthesam

Nice you can also write about anonymous functions specially the ones self invoking ypu dont even have to name them

Collapse
 
sumusiriwardana profile image
Sumudu Siriwardana

Yes, Will be having a different article on these areas 😊

Collapse
 
acoderbee profile image
Andrea I

This was just the right speed and detail. Now, I feel like I get it at this moment in time. Thanks so much!