官术网_书友最值得收藏!

  • JavaScript by Example
  • Dani Akash S
  • 205字
  • 2021-07-02 18:39:07

Arrow functions

Arrow functions are a cleaner and shorter way to define functions in JavaScript and they simply inherit the this object of its parent instead of binding its own. We'll see more about the this binding soon. Let's just look into using the new syntax. Consider the following functions:

let a = function(x) {
}
let b = function(x, y) {
}

The equivalent arrow functions can be written as:

let a = x => {}
let b = (x,y) => {}

You can see that () are optional, when we have to pass the only single argument to the function. 

Sometimes, we just return a value in a single line in our functions, such as:

let sum = function(x, y) {
return x + y;
}

If we want to directly return a value in our arrow function in a single line, we can directly ignore the return keyword and {} curly braces and write it as:

let sum = (x, y) => x+y;

That's it! It will automatically return the sum of x and y. However, this can be used only when you want to return the value immediately in a single line.

主站蜘蛛池模板: 万载县| 山东省| 溧阳市| 曲靖市| 绥芬河市| 江华| 资阳市| 富锦市| 资阳市| 武安市| 白朗县| 东乌珠穆沁旗| 普安县| 永新县| 花莲县| 扶余县| 新兴县| 高雄县| 阳城县| 海宁市| 轮台县| 平山县| 北辰区| 偃师市| 阜康市| 吉木萨尔县| 托克逊县| 饶河县| 泌阳县| 香格里拉县| 咸阳市| 仁布县| 泸溪县| 施甸县| 靖安县| 高邑县| 电白县| 梅河口市| 玛纳斯县| 建平县| 修武县|