JavaScript Arrow Functions & Regular Functions

JavaScript arrow functions & regular functions

Last Updated on April 3, 2024 by admin

JavaScript Arrow Functions & Regular Functions

 

In JavaScript, there are two types of functions i.e., regular functions and arrow functions. Let’s discuss….

 

JavaScript Arrow Functions :-

  • Introduced in ES6 and provide a easy and shorter way to create functions.
  • Allow for a more concise syntax, especially when the function has only one statement that returns a value.
  • Here’s the basic syntax of an arrow function:
    const functionName = (parameters) => {
    // function body
    };
  • Here’s an example of an arrow function to multiply two numbers:
    const multiply = (num1, num2) => num1 * num2;
  • Commonly used in modern JavaScript code for their simplicity and lexical scoping behavior.
  • Implicit return- If the function body consists of a single expression, the result of that expression will be implicitly returned.
  • These functions implicitly return the result of the expression if there are no curly braces around the function body.
  • Arrow Functions don’t have their own this binding. This makes arrow functions more predictable when dealing with this keyword.

JavaScript Regular Functions :-

  • Regular functions have their own this context.  And this is determined dynamically depending on how you call or execute the function.
  • Regular functions can be defined using the function keyword in two main ways: function declaration and function expression.
  • Function declaration example:
        function greet(who) {
    return `Hello, ${who}!`;
    }
  • Function expression example:
        const greet = function(who) {
    return `Hello, ${who}`;
    }
  • Regular functions can return anything and always run to completion after invocation.

 

     Note :-

  • If you want to learn Javascript Arrow & Regular Functions, Above are the key points which will help you to learn more about it.
  • Also, if you like this post or have any queries, Do let me know in the comments.
  •  Below are the resources to learn more about Javascript.

    Resources :-

 

Leave a Comment

Your email address will not be published. Required fields are marked *