Refugees Code

Functions in depth

A good programming language is more that just a mean for instructing a computer to perform tasks. It also help us think about the processes, by providing a way to combine simple ideas to form more complex ones.

Abstractions allow to write bigger pieces of code in terms of simpler elements. This is similar to a natural language, where we define new terms so we can refer to them later without giving all the details again. JavaScript has two means by which compound elements can be named and manipulated as units: functions and objects.

Functions are descriptions of the rules for manipulating data. They create abstractions around procedures, that is, a series of steps that we want to make on some given elements to produce a result. They take arguments and return a value, in a way similar to functions in mathematics.

In JavaScript, functions are first-class citizens, that is, they can be passed as arguments to other functions, and be returned from functions. Thus, in the same way than a variable can have a number or a string as a value, it can also have a function, and we can manipulate them in similar ways. This feature gives us a very powerful tool to write good, readable code.

Read the following articles carefully. Although hard at first, it's important that you start grasping at this concepts, they are the key aspects of the Javascript language.

Syntax of a function

📖 Javascript - functions

📖 Javascript functions - Understanding the basics

📖 Learn all about functions

Arrow functions

📖 6 ways to declare Javascript Functions

📖 ES6 Arrow functions: Fat and Concise Syntax in JS

📖 Javascript Arrow functions: How, Why, When (and WHEN NOT) to use them

📖 How to use Javascript arrow functions & this keyword

Scope

📖 A Simple explanation of Scope in Javascript

📖 Understanding Scope in Javascript

📖 Javascript Scope and Closures

📖 Understanding Variables, Scope, and Hoisting in Javascript

Closures

Now that you've learnt what scope is in Javascript, let's dive deeper into one of the key concepts of Javascript. Hard at first, once you get to understand it, you'll realise they've been around all this time, you just needed to meet them 😄

📖 A simple guide to help you understand closures in Javascript

📖 A simple explanation of Javascript closures

📖 Closures in Javascript for beginners

📖 Learn Javascript closures with code examples

Recursive functions

Functions can call themselves as part of their computation. For example, the following function computes the value of the factorial of a number (n! = n * (n - 1) * (n - 2) * ... * 1):

function factorial(n) {
if (n === 0) return 1;
return n * factorial(n - 1);
}
factorial(3); // returns 6

This function uses the fact that factorial(n) = n * factorial(n - 1). It works because at some point we will reach the call to factorial(0), which is 1, and from there the rest of the values can be computed.

Recursive calls can be very useful, especially when we have processes that traverse certain data structures such as trees.

📖 A quick intro to recursion in Javascript

Exercises

  1. Use Arrow Functions to Write Concise Anonymous Functions
  2. Write Arrow Functions with Parameters
  3. Global Scope and Functions
  4. Local Scope and Functions
  5. Global vs. Local Scope in Functions