avatarBalaji Dharma

Summary

The provided web content discusses ES6 Arrow Functions in JavaScript, detailing their syntax, comparison with regular functions, and various ways to declare them.

Abstract

The web content titled "ES6 Arrow Functions in JavaScript" introduces the concept of arrow functions as a concise alternative to regular function expressions in JavaScript. Introduced in ES6, arrow functions provide a shorter syntax and do not have their own this binding, which can be beneficial for certain use cases. The content illustrates the differences between regular and arrow functions, demonstrates how to convert regular functions to arrow functions, and showcases various arrow function declarations, including those without arguments, with a single argument, with multiple arguments, and those that return values directly without the need for a return statement. The article emphasizes the brevity and convenience of arrow functions in JavaScript programming.

Opinions

  • Arrow functions are considered a superior alternative to regular functions due to their concise syntax.
  • The lack of a this binding in arrow functions is highlighted as a significant difference from regular functions, which can lead to different behavior in certain contexts.
  • The author suggests that arrow functions can make JavaScript code cleaner and more readable, especially when dealing with functions that only consist of a single line of code.
  • The article implies that understanding the nuances between arrow and regular functions is crucial for JavaScript developers to write effective and error-free code.

ES6 Arrow Functions in JavaScript

Photo by Smart on Unsplash

Arrow functions are new alternative way to create functions in JavaScript. Arrow function expression were introduced in ES6.

Arrow functions actually allow us to create shorter function syntax compared to regular functions. Also Arrow function do not have their own binding of this for all the difference of regular and arrow functions read Difference between ES6 Arrow functions and Regular functions in JavaScript

Regular functions

function hello(name) {
  console.log(name);
}

We also write function as like below

const hello = function(name) {
  console.log(name);
}

Convert regular function to Arrow function

Time to convert your regular function to Arrow function expression

// removed function and added Arrow after argument
const hello = (name) => { 
  console.log(name);
}

Yes, now we created Arrow function. No more need to add function keyword in your JavaScript functions.

Different type of Arrow function declaration

Now again we going to short function expression.

Arrow Function without argument

const hello = () => { 
  console.log("Welcome");
}

Arrow Function with One argument

const hello = (name) => { 
  console.log(name);
}
// You may omit the parentheses for single argument
const hello = name => { 
  console.log(name);
}

Arrow Function with multiple argument

const hello = (a, b, c, d) => { 
  console.log(a);
}

Arrow function get more shorter if your function just return a value

Arrow Function with return value

const hello = name => name;
const hello = () => "Welcome";

That’s equal to

const hello = name => { 
  return name;
}
const hello = () => { 
  return "Welcome";
}
JavaScript
ES6
Arrow Functions
Javascript Basics
Js
Recommended from ReadMedium