Alpesh Maniya
What is different between normal function and arrow function in javascript?
By Alpesh Maniya in JavaScript on Jan 17 2024
  • Jayraj Chhaya
    Jan, 2024 29

    In JavaScript, there are two ways to define functions: normal functions and arrow functions. While both serve the same purpose of executing a block of code, there are some key differences between them.

    Syntax: The syntax for defining a normal function is function functionName(parameters) { … }, whereas the syntax for an arrow function is (parameters) => { … }. Arrow functions have a more concise syntax, especially when it comes to one-liner functions.

    Binding of this: One of the major differences between normal functions and arrow functions is how they handle the this keyword. In a normal function, the value of this is determined by how the function is called. However, in an arrow function, the value of this is lexically scoped and is inherited from the surrounding context. This means that arrow functions do not have their own this value and instead use the this value of the enclosing scope.

    Arguments object: Normal functions have access to the arguments object, which contains all the arguments passed to the function. However, arrow functions do not have their own arguments object. Instead, they inherit the arguments object from the enclosing scope.

    Use of new keyword: Normal functions can be used as constructors with the new keyword to create new objects. Arrow functions, on the other hand, cannot be used as constructors and do not have their own prototype property.

    Implicit return: Arrow functions have an implicit return feature. If the function body consists of a single expression, the result of that expression is automatically returned. This makes arrow functions useful for writing concise and readable code.

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS