Functions

Topic Covered In This section:

  1. What is a Function? Arrow Icon
  2. Function Declaration and Expression Arrow Icon
  3. Arrow Functions Arrow Icon
  4. Parameters and Return Values Arrow Icon
  5. Immediately Invoked Function Expressions (IIFE) Arrow Icon
  6. Scope and Closures Arrow Icon
  7. Higher-order Functions and Callbacks Arrow Icon

1. What is a Function?

Imagine you have a machine that makes sandwiches. Every time you want a sandwich, you just press a button, and it makes it for you. A function in JavaScript is like that button press: it’s a way to get something done without having to repeat all the steps every time.

In JavaScript, we write functions by naming them and telling them what to do. javascript Copy code

    function makeSandwich() {
    console.log("Here’s your sandwich!");
  }
                

Now, whenever we say makeSandwich(), it will print “Here’s your sandwich!” on the screen.

2. Function Declaration and Expression

There are two main ways to create functions in JavaScript:

3. Arrow Functions

Arrow functions are just a quicker way to write functions. They look a bit different, using => instead of function.

    const sayHello = () => {
    console.log("Hello!");
  };          
                

Arrow functions are especially useful when writing short functions. Here’s an even shorter version: javascript Copy code

    const sayHello = () => console.log("Hello!");
                

4. Parameters and Return Values

A function can take parameters, or special ingredients, that tell it what to use. Let’s say we want our sandwich-making machine to make sandwiches with different fillings. javascript Copy code

    function makeSandwich(filling) {
    console.log(`Here’s your ${filling} sandwich!`);
  }
                

If we say makeSandwich("peanut butter"), it will print “Here’s your peanut butter sandwich!” The filling is the parameter.

We can also make functions return values, meaning they give something back to us. For example: javascript Copy code

    function add(a, b) {
    return a + b;
  }
  
  const sum = add(5, 3); // sum will be 8
                

5. Immediately Invoked Function Expressions (IIFE)

An IIFE is a function that runs as soon as it’s created. It’s like a firework that goes off right after lighting. javascript Copy code

    (function() {
    console.log("This function runs immediately!");
  })();
                 

This can be useful if we want some code to run right away without calling it separately.

6. Scope and Closures

7. Higher-order Functions and Callbacks

A higher-order function is a function that either takes another function as a parameter or returns a function. It’s like using a helper to make things easier.

Callbacks are functions that get passed into other functions to be called back later.

For example, setTimeout is a higher-order function because it uses a callback to wait before running code: javascript Copy code

    setTimeout(() => {
    console.log("This message shows after 3 seconds");
  }, 3000);          
                

Higher-order functions are super useful in JavaScript, especially when working with data or making things happen at specific times.

  • JavaScript Topics