DEV Community

Jonathan Aguilar
Jonathan Aguilar

Posted on

Is there really a 'fun' in functions?

So, I know most people would argue that there is a lot of fun to be had with functions, but being a beginner with this language, and worse, failing to completely understand its in and outs, I must disagree wholeheartedly. Perhaps in a few weeks or months when I further my studying into Javascript, I will learn to appreciate it for what it is. Right now, it is a dread. But, what the little I know, it is most definitely interesting.
I will not sit and type here as if I am some guru of the language, because as I have mentioned, I am far from it. But functions, to me, are the heart of this language, and if you can wrap your head around how functions work, how there can be a function within a function, make anonymous functions, make a function with one purpose so that it can then be called back on later on in your code, that's definitely a good place to start.
A function usually starts with

Function
Enter fullscreen mode Exit fullscreen mode

a declaration, followed by parenthesis and curled brackets. Ultimately, looking like so:

Function() {
   //code here
}
Enter fullscreen mode Exit fullscreen mode

OR, it can be turned into an arrow function, like so:

placeholderName() => //code here for what is to be returned
Enter fullscreen mode Exit fullscreen mode

In the last example, curled brackets can be used if there are multiple statements to be returned, meaning if the function has multiple things it can do. The word 'Function' is also eliminated since we use '=>' instead, which Javascript understands is a function declaration, rightly called an arrow function. Either of these methods work and I guess the important thing to note with these is what belongs in the code blocks, which as I mentioned before, are descriptions of what the function can do; statements. Within these we can have simple console.logs, we can run loops, etc. There's plenty that can be done and this can be its own blog post to be honest.
But wait, hold on. What about the parentheses? Can that just be left as is. Depending what you want your function to do is the answer. If you put something within your parentheses, those are called parameters, which are arguments that you will pass through the function.
Being as this is my first blog post, I deliberately want to keep it simple and also write on a simple topic. Hopefully at some point in the future I can come back to this initial post and just laugh at all the trouble I am having with Javascript and realize how far I have come. I hope.

Top comments (0)