cover-img

Arrow Functions in JavaScript

20 January, 2023

11

11

1

Arrow functions are a concise way to write function expressions in JavaScript. They are instrumental when you write a small function as an argument to another function.

Here is the syntax for an arrow function:

(parameters) => { statements }

For example, the following code defines a function add that takes two arguments, x and y, and returns their sum:

const add = (x, y) => {
return x + y;
};

If the function body consists of a single statement, you can omit the curly braces and the return keyword:

const add = (x, y) => x + y;

You can also use arrow functions to create a function that does nothing:

const noop = () => {};

Arrow functions do not have their own this, arguments, super, or new.target bindings are often used when you don't need these features.

The arrow function and the this keyword

In JavaScript, the this keyword refers to the object that owns the current execution context. In the global execution context, this refers to the global object (either window in a browser or global in Node.js). In a regular function, the value of this is determined by how the function is called.

In contrast, the value of this inside an arrow function is determined by the surrounding lexical context. In other words, the value of this inside an arrow function is the same as the value of this outside the function.

Here's an example that demonstrates this difference:

const obj = {
message: 'Hello',
sayHello: function() {
console.log(this.message);
},
sayHelloArrow: () => {
console.log(this.message);
}
};

obj.sayHello(); // Output: 'Hello'
obj.sayHelloArrow(); // Output: undefined

In the example above, the regular function sayHello logs the message property of the obj
object, as expected. However, the arrow function sayHelloArrow logs undefined, because the value of this inside the arrow function is the global object (which does not have a message
property).

Arrow functions are helpful in JavaScript for a number of reasons:

  1. Concise syntax: As I mentioned earlier, arrow functions have a concise syntax, which makes them easier to write and read. This is especially useful when you need to write a small function as an argument to another function.
  2. No this binding: As I mentioned earlier, arrow functions do not have their own this binding, which means the value of this inside an arrow function is the same as the value of this outside the function. This can be helpful in situations where you want to access the value of this from an enclosing scope.
  3. No arguments object: Arrow functions do not have their own arguments object, which means you cannot use the arguments object inside an arrow function to access the arguments passed to the function. Instead, you can use the rest parameters syntax (...args) to access the arguments as an array.
  4. Can't be used as constructors: Arrow functions cannot be used as constructors, which means you cannot use the new operator with arrow functions. This can be helpful in situations where you want to prevent a function from being used as a constructor.

11

11

1

ShowwcaseHQ
Showwcase is where developers hang out and find new opportunities together as a community

More Articles