JavaScript Arrow Functions

In this tutorial, you will learn about JavaScript arrow function with the help of examples.

Arrow function is one of the features introduced in the ES6 version of JavaScript. It allows you to create functions in a cleaner way compared to regular functions. For example,

This function

// function expression
let x = function(x, y) {
   return x * y;
}

can be written as

// using arrow functions
let x = (x, y) => x * y;

using an arrow function.


Arrow Function Syntax

The syntax of the arrow function is:

let myFunction = (arg1, arg2, ...argN) => {
    statement(s)
}

Here,

  • myFunction is the name of the function
  • arg1, arg2, ...argN are the function arguments
  • statement(s) is the function body

If the body has single statement or expression, you can write arrow function as:

let myFunction = (arg1, arg2, ...argN) => expression

Example 1: Arrow Function with No Argument

If a function doesn’t take any argument, then you should use empty parentheses. For example,

let greet = () => console.log('Hello');
greet(); // Hello

Example 2: Arrow Function with One Argument

If a function has only one argument, you can omit the parentheses. For example,

let greet = x => console.log(x);
greet('Hello'); // Hello 

Example 3: Arrow Function as an Expression

You can also dynamically create a function and use it as an expression. For example,

let age = 5;

let welcome = (age < 18) ?
  () => console.log('Baby') :
  () => console.log('Adult');

welcome(); // Baby

Example 4: Multiline Arrow Functions

If a function body has multiple statements, you need to put them inside curly brackets {}. For example,

let sum = (a, b) => {
    let result = a + b;
    return result;
}

let result1 = sum(5,7);
console.log(result1); // 12

this with Arrow Function

Inside a regular function, this keyword refers to the function where it is called.

However, this is not associated with arrow functions. Arrow function does not have its own this. So whenever you call this, it refers to its parent scope. For example,

Inside a regular function

function Person() {
    this.name = 'Jack',
    this.age = 25,
    this.sayName = function () {

        // this is accessible
        console.log(this.age);

        function innerFunc() {

            // this refers to the global object
            console.log(this.age);
            console.log(this);
        }

        innerFunc();

    }
}

let x = new Person();
x.sayName();

Output

25
undefined
Window {}

Hope this helps in improvising your JavaScript code for your Power Platform Implementations

Happy CRM’ing.

Cheers,

PMDY

Advertisement

Author: Pavan Mani Deep Y

Technology geek who loves sharing my leanings, quick tips and new features on Dynamics 365 & related tools, technologies. An Azure IOT Enthusiast....

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

ECELLORS CRM Blog

Sharing thoughts and challenges on Power Platform & Azure along with respective solutions put together...

SharePains by Microsoft MVP Pieter Veenstra

Microsoft 365, Power Platform, SharePoint, Teams, Azure and Dynamics

D365 Demystified

A closer look at Microsoft Dynamics 365.

Vicky Rodgers - Microsoft Dynamics 365

Everything Microsoft Dynamics 365 for Customer Engagement

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

Microsoft Dynamics 365 UK MCT

All about Microsoft Dynamics 365

High Voltages

Arduino | ESP | Raspberry pi | IoT

Mihail's space

Just another WordPress.com site

Arun Potti's MS CRM blog

Microsoft Dynamics CRM

Sander van de Velde

Microsoft MVP Azure | IoT Platform Architect | Speaker about IoT | Let me add some value

Power Platform Blog

Author : Dharanidharan Balasubramaniam

That API Guy

Making the world a better place one PowerApp/Flow at a time!

Dynamics Back Page

Tips, Tricks, and General Musings on Dynamics 365 (CRM), the Power Platform, Power Apps, Power Automate, Power BI and related technologies

Azure Integration services for Dynamics 365

Azure Integration services for Dynamics 365 Unified Operations, Logic Apps, Azure Functions, REST

Power Maverick

Incredible journey into #Dynamics365, #CDS, #PowerApps #Azure and all good stuff that #Microsoft releases

Unleashing CRM

My Technical Thoughts about Microsoft Dynamics CRM- Unleashed

Srikanth Alluri

My experiences in & around Power Platform + Azure

Debajit's Power Apps & Dynamics 365 Blog

All about Power Apps & Dynamics

Rajeev Pentyala - Microsoft Power Platform

Sharing my knowledge on Power Platform, Dynamics 365, Azure & .Net Stack

Bansal Blogs - Dynamics 365, PowerApps, Microsoft Flows, Power BI

Programming demands a lot more than simple mastery in tools and technologies

%d bloggers like this: