Sitemap
David Fellows

David Fellows

Optimistic Developer Based in NYC

Understanding JavaScript’s essential functions

5 min readDec 4, 2019

--

Press enter or click to view image in full size
from Fireship youtube tutorial

Switching from programming in Ruby to learning JavaScript was a jarring change for me. I was used to clear, concise, syntax and keywords that gave way to a never ending stream of parenthesis and brackets. Ultimately what I found the most challenging was the flexible and diverse use of functions. Unlike other languages JS functions are higher order functions meaning they can be used as an argument for another function or can return yet another function. How many times do you think I’m going to say function in this post?

Anyways, while it can be frustrating at first, JavaScripts functions allow for a high degree of abstraction and enable us to break programs into nice compartmentalized chunks. Mastering their various forms is necessary for any serious JS developer. The topics listed below barely skim the surface but provide beginning developers with a launching point for more research. Enjoy and happy learning!

1. Function declarations

You probably recognize function declarations (also called function statements) from your earliest lessons in Javascript. They can be identified easily because they start with the keyword function, and a name for the function. These are followed by a list of parameters and a block/statement that defines what the function does. In Javascript, these functions are stored as objects that can be invoked (or called) at a later time.

Example of function declaration

2. Function expressions

Function expressions are quite similar to declarations and can be mistaken if you don’t look carefully. The key difference in syntax is that expressions do not need a function name — which when omitted becomes an anonymous function. By themselves anonymous functions are not syntactically correct so must be used in other contexts.

Not allowed! Whenever JavaScript sees a function keyword it expects a definition to take place. Adding a “!” or “void” at the beginning would create a valid statement by treating whatever comes after as an expression.

After the expression is assigned to a variable it can be called by using the variable name like a normal function. There are, however, several differences and advantages. Unlike declarations, expressions will not be hoisted, meaning they cannot be used before the JS interpreter reaches the line of code where they are defined. This allows them to keep information about the local variables from the scope where they are defined.

3. Closures

Closures are one of the most powerful features of Javascript and far outside the scope of this post (ha.. ha). However complex they may be, they can be defined simply as one function nested within another function. They are extremely useful because they allow you to privatize data that is related to that function. This is called lexical scope and sets the scope of a variable or function so that it can only be called from within the environment (block) it was defined. The outer function cannot read variables from the inner function which creates the closure.

Some common uses are:

  • Callback functions
  • Cleaning up code
  • Avoiding polluting the global scope
  • Emulating private methob . ds

4. Callback functions

Callback functions are functions that are passed as an argument to another function and then called at a later time, hence the name. Callbacks really shine when creating asynchronous javascript code. In situations when you are communicating with external data (APIs) they most likely will not respond instantaneously which is a very long time for your code to wait! When using callback functions the code that is executed within them is put in a secondary stack to be executed(a lower level topic that is great to research), allowing the rest of your other code to continue. A bonus is rather than having one large nested closure you can split your code into bite size, logical chunks. Just be careful to avoid too many nested callbacks or it can be extremely difficult to debug (commonly referred to as callback hell).

Press enter or click to view image in full size
Callback hell

5. Immediately invoked function expressions (IIFE)

IIFE’s, pronounced as “iffy”, are as you might expect, functions are invoked when they are created and only once. One of the most recognizable examples can be seen in many frameworks/libraries such as jQuery. To protect their variables the entire codebase is wrapped in an IIFE. See an extremely simplified example below. Note the extra set of parentheses on line 3:

There are several difference syntactic versions of IIFE’s that could be a topic for future posts. IIFE’s were originally used to control scope but have since fallen out of popularity since the introduction of block-scoped variables in ECMAScript 2015. Understanding them at a higher level is useful, however, because there are still developers utilizing them for advanced cases and performance boosts.

Get David Fellows’s stories in your inbox

Join Medium for free to get updates from this writer.

Examples can be found here: https://mariusschulz.com/blog/use-cases-for-javascripts-iifes

6. Arrow functions

Arrow functions were one of the most anticipated changes of ES6 and serve two primary functions: drastically reducing the size of a function and managing the binding of this. They are best approached from these two angles. For the first subject, I’ll let the code speak for itself.

When only one parameter is given, drop those parenthesis! Single line block? Who needs curly braces?! All arrow functions are anonymous. Who needs a name?! Such beauty..

Where was I? Ahem.. As JavaScript began to expand into an object oriented language, especially in modern frameworks such as React, developers ran into a problem. Until arrow functions, every function had its own this value, which captured this value within its local environment. Arrow functions allowed it to escape that context and become more abstract, referred to the greater class that function resides in. Cool stuff.

P.S. function was said 46 times.

--

--

David Fellows
David Fellows

Written by David Fellows

Optimistic Developer Based in NYC