beginner

Functions


Functions help keep your code organized and reusable. Instead of writing the same code over and over again, you can write it once inside a function and then reuse that function whenever you need that functionality.

Defining Functions

A function is defined using the function keyword, followed by a name and a set of parentheses containing parameters. The function body is enclosed in curly braces.

function greet(name) {
  console.log(`Hello, ${name}!`);
}

greet("Alice"); // Hello, Alice!

Default and Optional Parameters

You can assign default values to parameters, making them optional. If a value is not provided, the default value will be used.

function greet(name = "User") {
  console.log(`Hello, ${name}!`);
}

greet(); // Output: Hello, User!

Return Statement

The return statement is used to send a value back to the caller of the function.

function square(number) {
  return number * number; // 4 * 4
}
let result = square(4); // Returns 16

Anonymous Functions

Anonymous functions are functions without a specified name. They can be assigned to variables or used as arguments for other functions. If you think about it, same as normal functions. But there are some specifics that we will cover in later posts.

let add = function (a, b) {
  return a + b;
};

let result = add(3, 5); // Result: 8

IIFE (Immediately Invoked Function Expressions)

IIFE is a way to execute a function immediately after its creation. It helps create a private scope for variables and prevents polluting the global scope. We’ll talk about scopes in upcoming posts.

(function () {
  let message = "I am an IIFE.";
  console.log(message);
})(); // I am an IIFE.

You’ll notice it is a normal function wrapped inside (function...)(). The second () is why it is immediately invoked/called.

First-Class Citizens

In NodeJS, functions are first-class citizens. This means they can be treated like any other variable: assigned to variables, passed as arguments, and returned from other functions.

let square = function (x) {
  return x * x;
};

let processNumbers = function (operation, num) {
  return operation(num);
};

let result = processNumbers(square, 4); // Result: 16

Callback Functions

Callback functions are functions passed as arguments to other functions and executed later.

function process(data, callback) {
  // Process the data
  callback(data + " there");
}

function printText(text) {
  console.log(text);
}

process("Hello", printText); // Hello there

We have printTextfunction defined that takes in an argument and console.log it. Then we pass in that function to another function process as a second parameter. We call this parameter callback (but any name would work) and then we run that function inside the process. Because we also passed in Hello as data and we concatenate strings before sending it to printText we get result Hello there.

What now?

Try and create a very simple shopping app where you have a user, some products, shopping cart and add products to it using a function. Try to use (almost) everything we learned so far. If you need help here is a list:

  • variables
  • objects
  • arrays
  • conditionals
  • for loops
  • functions

Feel free to add whatever functionality you want, the more you practice, the better.

Next Higher order functions