Sitemap
Michael Davis

Michael Davis

Expressing Functions in JavaScript

4 min readJun 20, 2022

There are many different blocks of code that programmers write. The first one most people learn and maybe the most useful is a function. It is flexible, comes in many different forms, and is a key part of code in controlling flow. I will be speaking from a JavaScript background but also speaking in general and will provide information applying to most uses of function across different languages.

What Is A Function

A function is a block of code that when invoked performs a task. A function is usually defined with keyword function, a name for that function, a parameter that the function will accept, and the code in between brackets that will run when the function is called. This is called a function declaration. Functions are invoked by writing out its name followed by a set of parenthesis.

Here doSomething is declared above and every time doSomething is invoked it will then run the code under it.

Functions are known for their reusability. They clean up code but removing the need to repeat lines of code and instead writing out the function name every time the code is needed. With that the code can also be called from different places which gives programming more control at operating how the code flows. Being able to group tasks and place them elsewhere in a set of code can be beneficial to working around other aspects.

These are some of their many great attributes but what I originally wanted to talk about is one of the best things about functions and that their flexibility. In the image above I merely used some lines of code that do not really do anything but syntactically it is correct. You’ll see that there is no parameter between the parenthesis. Function do not need to pass a parameter to work. The could also receive multiple parameters. The function would simply run the code. There are other ways to run a function other than the basic way I mentioned in the beginning.

A Function Expression

Get Michael Davis’s stories in your inbox

Join Medium for free to get updates from this writer.

Functions can be called without each of the attributes listed before. A common way of writing a function is an expression in another piece of code.

Here our function doSomething is awaiting another function to be passed as a parameter that would be invoked after doSomething is called. The function inside is a callback function which is basically just a function that is not explicitly called on it own. Usually a callback is declared somewhere in the code and called back through being referenced somewhere else in the code. Here we simply express the function directly in the original function and because of this the function does not need to be declared with a name. We can also pass a function expression into a variable.

It is functionally the same as declaring the function itself but here we declare the variable and pass the function as a value.

Arrow Function

We can strip more in certain situations by using an arrow function. An arrow function basically only takes a parameter, the arrow itself and what will happen to that parameter.

Arrow Functions are a great and clean way to add functionality in an expression format. It uses an arrow to basically say use this parameter to run this code. They are however, a trade off of a normal function expression in that their simplicity limits how they can be called even further and disqualifies them from many different methods. Also they don't really have a scope and are immediately invoked as soon as they are ran.

There are even more ways to construct and declare functions. Many of these ways work together. Functions can callback other functions and use each other as arguments. They can be dynamically used in many different scenarios.

--

--