Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 3480

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T03:06:07+00:00 2024-11-26T03:06:07+00:00

Understanding Arrow Functions in JavaScript ES6 — Full Guide

  • 61k

JavaScript is the language that powers the web. The web is certainly dominated by it. You may be wondering what is ES6 means? You maybe have seen ES4, ES5, ES6… terms.

Javascript wasn’t even always called Javascript. In fact, when Brendan first created it, it was actually called LiveScript, and then the people at Microsoft decided to try and reverse engineer the program and they ended up with something called Jscript. And so there were all the slightly different versions of Javascript that were being run on the web and it was starting to get quite confusing.

So the Europeans did what Europeans do best and they decided to standardize the language. And that’s where you get the ECMAscript from. And that stands for the European Computer Manufacturers Association Script. And the only reason why this is interesting is that often you’ll see different versions of Javascript not referred to as JS5 or JS6 but as ES6 or ES7, and the ES comes of course from ECMAscript. So Arrow functions were introduced in the ES6 version of JS.

Alright so enough history.
Let’s see what is Arrow function and how it works!

This is quite easy when you understand what will happen there. But if you don’t know what is the concept behind that Arrow functions. I'm sure you will already be confused what’s the meaning of that two braces, the arrow, and the two curly brackets.

Simply Arrow functions is also a normal function. But it was simplified to reduce lines of code.

Steps of Simple Function ➜ Arrow Function

function myFunction(a, b){   return a * b; } 
Enter fullscreen mode Exit fullscreen mode

So, this is a normal javascript function that returns the product of the two parameters a and b.

but we can write this function without the function name. That calls anonymous functions in JS.

function (a, b){   return a * b; } 
Enter fullscreen mode Exit fullscreen mode

Now, you may be wondering without a function name, how we call that function, without calling the function what is the use of that function. Okay, I agree but these anonymous functions don’t write for calling purposes.

For example, assume that we have a button to get the product of two numbers. To do that we have to write a function for onClick. So directly we can write like this with an anonymous function.

<button onClick={function(a,b){      return a*b; }}>     Get Product </button> 
Enter fullscreen mode Exit fullscreen mode

with the clarification of that, Now let's see the next step.

We can remove the 'function' keyword as well. let's remove the function keyword.

(a, b){          //This will give an error   return a * b; } 
Enter fullscreen mode Exit fullscreen mode

but this will give an error, so after removing the function keyword you need to put an arrow.

(a, b) => {   return a * b; } //let's write it in one line (a, b) => {return a * b;} 
Enter fullscreen mode Exit fullscreen mode

As you can see, this is the basically Arrow function in JS. Arrow function is also a function that is simplified.
Just remove the function name and function keyword. Instead of the function keyword, we need to put an arrow.
Hope you now got the idea about Arrow functions in JavaScript!

So, now look at the previous example. We can simply write that as the following.

<button onClick={(a,b)=>{return a*b;}}>     Get Product </button> 
Enter fullscreen mode Exit fullscreen mode

Wait, another important fact!
If your function only has one line, you do not need to wrap that line with curly braces. In this case, no need to wrap return a*b with curly braces and you do not need to write return keywords as well. The compiler knows that it should be returned.

<button onClick={(a,b)=> a*b}>     Get Product </button> 
Enter fullscreen mode Exit fullscreen mode

So simply we can write our Arrow function like this. Now you can get some idea of why arrow functions are used in JS. It reduces a lot of lines of code when developing a complex system.

Likewise, there are a lot of ways to write arrow functions in JavaScript. Now let’s quickly get informed on the ways you can write an Arrow Function.

Cheat Sheet of Arrow Functions in JS.

Cheat Sheet - Arrow Functions ES6

This is the cheat sheet of the Arrow Functions ES6.
Maybe when you see this, you feel like what the heck is this, like that… 😀 Don’t worry I’ll explain everything in this cheat sheet. let’s gooo…

Implicit vs Explicit Return

First, you need to understand what is Implicit Return and Explicit Return.

With normal functions, if you want to return something, you have to use the return keyword. Arrow functions also have that. When you use the return keyword, it's called an explicit return.

However, the arrow functions allow something called implicit return where the return keyword can be skipped. Let's look at some examples.

Example A: Normal Function

const sayHi = function(name) {   return name } 
Enter fullscreen mode Exit fullscreen mode

Example B: Arrow Function with Explicit Return

// Multi-line const sayHi = (name) => {   return name }  // Single-line const sayHi = (name) => { return name } 
Enter fullscreen mode Exit fullscreen mode

Example C: Arrow Function with Implicit Return

// Single-line const sayHi = (name) => name  // Multi-line const sayHi = (name) => (   name ) 
Enter fullscreen mode Exit fullscreen mode

Notice the difference? When you use curly braces {}, you need to explicitly state the return. However, when you don't use curly braces, the return is implied and you don't need it.

There’s actually a name for this.
When you use curly braces like in Example B, it’s called a Block Body.
And the syntax in Example C is called a Concise Body.

Simply,

  • Block body is where you use curly braces and have an explicit return.
  • The concise body is where you don’t use curly braces, and you skip the return keyword.

Alright, another thing about Parentheses () of Arrow functions.

First of all, parentheses mean two braces that we put in functions to wrap parameters. Sometimes it will be empty.

So, In normal functions we always need parentheses. But in Arrow Functions we do not need parentheses if there is only one parameter.

— Parentheses are optional for a SINGLE parameter —

Single Parameter

— Parentheses are required for MULTIPLE parameters —

Multiple Parameter

IMPORTANT — Returning Objects

However, when you return an object in Arrow functions, you should put parentheses even you have only one line in the function.

const me = () => { name: "Gayan" };  me(); //Output --> undefined 
Enter fullscreen mode Exit fullscreen mode

The above one is wrong ❌

const me = () => ({ name: "Gayan" });  me(); //Output --> { name: "Gayan" } 
Enter fullscreen mode Exit fullscreen mode

This is correct ✅

That’s all about it. I hope you got the idea about Arrow Functions ES6 and find this post useful, and I would love to see your feedback about the article. Or if you have any questions please put them all in the comment section, I will reply to you all.

Follow me on Twitter @gayankodX for more updates!

arrowfunctionsjavascriptwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.