DEV Community

Dainwi Kumar
Dainwi Kumar

Posted on

Easy JavaScript with Arrow Functions!

Do you want to make your JavaScript code more concise? Look no further than arrow functions! Arrow functions are a modern feature in JavaScript that allows you to write functions in a simpler and more streamlined way. Let's dive in and see how they work.

An arrow function looks like this:

const add = (a, b) => {
  return a + b;
};
Enter fullscreen mode Exit fullscreen mode

In this example, addis a function that takes two parameters, a and b, and returns their sum. The => symbol is what makes this an arrow function. It's like saying "take these parameters and do this operation".

Arrow functions are especially useful for shortening code when passing functions as arguments to other functions. For example, suppose you have an array of numbers and you want to double each number. You could use the map function along with an arrow function like this:

const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(num => num * 2);
Enter fullscreen mode Exit fullscreen mode

In this code, map takes each element of the numbers array and applies the arrow function to it, doubling each number. The result is stored in the doubled array.

Another benefit of arrow functions is that they have a shorter syntax for certain cases. If the function body consists of only a single expression, you can omit the curly braces and the return keyword. For example, the add function from earlier can be written even more concisely like this:

const add = (a, b) => a + b;
Enter fullscreen mode Exit fullscreen mode

This is particularly handy when writing small, simple functions.

So there you have it! Arrow functions are a powerful tool in modern JavaScript for writing cleaner, more readable code. Give them a try in your next project and see how they can simplify your code!

Top comments (6)

Collapse
 
helio609 profile image
Helio

For farther more, can you explain the arrow function scope for me. I really don't know the diffirence between the normal function and arrow function.

Collapse
 
dainwi profile image
Dainwi Kumar

Think of a function as a recipe. When you need to do something again and again, you write a recipe for it, and whenever you want to do that thing, you just follow the recipe.

Scope is like a rule about where things can be seen or used. It's like knowing where your toys are allowed to be played. Some toys are only for the living room, some are only for your bedroom.

In JavaScript, functions have their own scope, which means they can see and use certain things in the code.

Now, the difference between a normal function and an arrow function is how they see and use things around them.

A normal function has its own scope, and it can see and use things from its scope and also from the scope around it. It's like a kid who can play with toys from their own room and also from the living room.

But an arrow function is a bit different. It also has its own scope, but it can only see and use things from the scope around it. It can't have its own toys; it can only borrow toys from the living room, for example.

So, in simple words:

  • Normal functions can see and use their toys and toys from the living room.
  • Arrow functions can only borrow toys from the living room; they can't have their toys.

I hope that helps you understand the difference between normal functions and arrow functions! If you have any more questions, feel free to ask!

Collapse
 
miketalbot profile image
Mike Talbot ⭐ • Edited

You know, I like the style of your answer but it isn't 100% correct.

Arrow functions can have their "own toys" - it's fine. An arrow function with { and } is fundamentally exactly the same as a function in this respect.

The fundamental difference is in what this means inside a the body of the function. Arrow functions don't have their own this - they use the this of the enclosing scope, which is handy in classes, especially when defining nested functions (like doing a .map() inside a class method). In a nested function the this is not the this of the class, so you have to jump through some hoops, an arrow function simplifies the code by allowing you to still use the class's this.

Thread Thread
 
helio609 profile image
Helio

Wow! Super easy to understand, thank you.

Collapse
 
ezekiel_77 profile image
Ezekiel • Edited

So if functions are recipes that create stuff

Callback functions and Closures are recipes that ____

Thread Thread
 
miketalbot profile image
Mike Talbot ⭐

Firstly - all functions in JavaScript come packaged with a closure. The closure means that, even if the function is executed later, it has a reference to the variables that are in scope where the function is defined.

Here's a simple case where we use the closure to access a variable from the outer function's scope. Both of these examples are using a closure to access someCalculation.

function doStuff(someArrayOfIntegers, aNumber) {
      const someCalculation = someArrayOfIntegers.length / aNumber
      return someArrayOfIntegers.map(i=>i+someCalculation)
}

function doStuffUsingAFunction(someArrayOfIntegers, aNumber) {
      const someCalculation = someArrayOfIntegers.length / aNumber
      return someArrayOfIntegers.map(processEntry)

     function processEntry(someInteger) {
          return someInteger + someCalculation
     }
}
Enter fullscreen mode Exit fullscreen mode

We are also using callbacks here. The function map takes a callback that should return a value that will be used to populate a new array. As we can see, that function has access to the outer function's scope.

A more advanced use of closures would be to return the function and closure to the caller so that even though the outer function has finished, the returned function still has access to its variables.

     function makeCounter() {
           let count = 0
           return ()=>count++
     }

     const myCounter = makeCounter()
     console.log(myCounter()) // 0
     console.log(myCounter()) // 1
     console.log(myCounter()) // 2

Enter fullscreen mode Exit fullscreen mode

This makeCounter makes a counter where any other code cannot mess with the current count, it is totally private to the returned function. The returned function is just incrementing a variable in its parent's scope and returning the value.