Easy To Learning
  • Login
  • Sign Up
  • Q&A
  • Home
  • Php Tutorial
  • MySql Tutorial
  • Object-Oriented Programming
  • Java Tutorial
  • HTML Tutorial
  • React JS Tutorials
  • Free Projects
  • Blogs & Articles
  • Others
    • About Us
    • Contact Us
Arrow functions
Easy To Learning

Education Needs Complete Solution

Online Ask Question Question And Answer
  1. Home
  2. React JS Tutorials
  3. Arrow functions

React JS Tutorial

Modern Javascript Core Concepts

  • Modern Javascript Core Concepts
  • Variables
  • Arrow functions
  • Rest and spread
  • Object and array destructuring
  • Template literals
  • Class
  • Callbacks
  • Promise
  • Async Or Await
  • ES Modules

React Js Basic Fundamentals

  • React JS Introduction
  • How to install React

Arrow functions

Summary: Arrow functions were introduced in ES6 / ECMAScript 2015, and since their introduction they changed forever how JavaScript code looks (and works).

Arrow functions were introduced in ES6 / ECMAScript 2015, and since their introduction they changed forever how JavaScript code looks (and works).

Syntax:

Normal Function Syntax

const myNormalFunction = function() {
  //...
}

Arrow Function Syntax

const myArrowFunction = () => {
  //...
}

Convert Normal To Arrow Function Syntax

const yourFunction = function() {
  //...
}

to 

const yourFunction = () => {
  //...
}

If the function body contains just a single statement, you can omit the brackets and write all on a single line:

const yourFunction = () => yourDoSomething()

Parameters are passed in the parentheses:

const yourFunction = (param1, param2) => yourDoSomething(param1, param2)

If you have one (and just one) parameter, you could omit the parentheses completely:

const yourFunction = param => yourDoSomething(param)


Implicit return

Arrow functions allow you to have an implicit return: values are returned without having to use the return keyword.

It works when there is a one-line statement in the function body:

const yourFunction = () => 'test'

yourFunction()

Output: //'test'

Example, when returning an object, remember to wrap the curly brackets in parentheses to avoid it being considered the wrapping function body brackets:

const yourFunction = () => ({ value: 'test' })

yourFunction()

Output: //{value: 'test'}

How this works in arrow functions

It’s important to clarify this concept because arrow functions behave very differently compared to regular functions.

When defined as a method of an object, in a regular function this refers to the object, so you can do:

const car = {
  model: 'Fiesta',
  manufacturer: 'Ford',
  fullName: function() {
    return `${this.manufacturer} ${this.model}`
  }
}
calling car.fullName() will return "Ford Fiesta".

The this scope with arrow functions is inherited from the execution context. An arrow function does not bind this at all, so its value will be looked up in the call stack, so in this code car.fullName() will not work, and will return the string "undefined undefined":

const car = {
  model: 'Fiesta',
  manufacturer: 'Ford',
  fullName: () => {
    return `${this.manufacturer} ${this.model}`
  }
}

Due to this, arrow functions are not suited as object methods.

Arrow functions cannot be used as constructors either, when instantiating an object will raise a TypeError.

This is where regular functions should be used instead, when dynamic context is not needed.

This is also a problem when handling events. DOM Event listeners set this to be the target element, and if you rely on this in an event handler, a regular function is necessary:

const link = document.querySelector('#link')
link.addEventListener('click', () => {
  // this === window
})

const link = document.querySelector('#link')
link.addEventListener('click', function() {
  // this === link
})

 

Series: React JS Tutorials Hub
All Topics
Free Download Projects
Previous Chapter
Next Chapter

Ask a Question

Have a coding query or doubt? Ask here and get help from our community.

500 characters remaining
Submitting...
Free Download Projects
Join Telegram Group Join WhatsApp Group

Member Sign In

Forgot?
Signing in...

Don't have an account? Register here

Create Free Account

Register Captcha
Registering...

Already have an account? Sign In

Ask a Question

500 characters remaining
Submitting...

About Easy To Learning

Easy To Learning is a comprehensive programming learning portal offering free, practical, developer-driven tutorials for PHP, Java, Python, Spring, MySQL, CSS, Bootstrap, CakePHP, and more.

Explore community-contributed questions and answers, and download ready-to-run source code projects.

Quick Links

  • Home
  • Questions & Answers
  • Download Projects
  • Blog & Articles
  • Member Login

Community

  • Telegram Group
  • WhatsApp Community
  • Ask a Question
  • Join as Member

Connect With Us

Stay updated with our newest programming tutorials and free open source projects.

© 2026 Easy To Learning. All Rights Reserved.