Introduction to Javascript Arrow Functions

Photo by Jexo on Unsplash

Introduction to Javascript Arrow Functions

Β·

3 min read

Arrow functions provide a concise syntax for writing functions, making code more streamlined and expressive. In this blog post, we will delve into the world of arrow functions, understand how and why they are used, and explore their inner workings under the hood.

The Syntax and Purpose of Arrow Functions:

Arrow functions offer a shorter and more elegant way to define functions compared to traditional function expressions. The syntax is straightforward, consisting of a pair of parentheses (optional for single parameter), an arrow (=>), and the function body.

Here's an example of a traditional function expression:

function doSomething() {

}

The equivalent arrow function:

const doSomething = () => {

}

Where do I need arrow functions?

  • Arrow functions are particularly useful in scenarios where concise, one-liner functions are needed.

  • They excel at providing a clear and concise way to express simple operations or callbacks.

  • This brevity leads to more readable code, reducing the noise and allowing the developer to focus on the core logic.

How will export look:

export default const function doSomething() {

}
export const doSomething = () => {

}

Where do you see this: in React!

const MyComponent = () => {
 // some component
}

Anonymous function in react:

<button onClick = {() => {
    "Do something"
    }
}>
    Hi 
</button>

Lexical Scoping and the "this" Keyword:

One significant difference between arrow functions and traditional functions lies in how they handle the "this" keyword. In traditional functions, "this" is dynamically scoped based on how the function is called. In arrow functions, "this" is lexically scoped, meaning it retains the value of the enclosing context.

Consider the following example:

const person = {
  name: 'John',
  greet: function() {
    setTimeout(function() {
      console.log('Hello, ' + this.name); // undefined
    }, 1000);
  }
};
person.greet();

In the above code snippet, using a traditional function as the callback in setTimeout results in "undefined" because the "this" inside the callback refers to the global object (or "undefined" in strict mode). This is a common source of confusion for many JavaScript developers.

Now, let's modify the code to use an arrow function:

const person = { 
    name: 'John', 
    greet: function() { 
        setTimeout(() => { 
            console.log('Hello, ' + this.name); // John 
        }, 1000); 
    } 
}; 
person.greet();

With the arrow function, "this" refers to the lexical scope of the enclosing "greet" function, allowing us to access the "name" property correctly. This behavior simplifies the understanding and usage of the "this" keyword in JavaScript.

Under the Hood: How Arrow Functions Work

  1. To comprehend how arrow functions work under the hood, it's essential to understand their relationship with the this keyword and how they differ from traditional functions.

In JavaScript, every function declaration creates a new scope. Traditional functions have their own this value, which is set dynamically when the function is called. This dynamic binding of this can cause confusion, especially when nested functions or callbacks are involved.

On the other hand, arrow functions inherit the this value from their lexical (enclosing) scope. They do not bind their own this but rather retain the this value of the surrounding code. This behavior is achieved by using the [[Environment]] internal slot, which allows arrow functions to access variables and this from their lexical environment.

  1. The absence of a prototype property is another notable characteristic of arrow functions. Traditional functions have a prototype property that can be used with constructor functions and object-oriented programming in JavaScript. However, arrow functions lack this property, making them unsuitable for use as constructors.

Conclusion:

  • Arrow functions in JavaScript provide a concise and expressive syntax, allowing developers to write cleaner and more readable code.

  • They simplify function definitions, reduce the verbosity of callbacks, and handle the this keyword more intuitively.

Β