From 5980f92665da0e7236016aa180061619f529dab2 Mon Sep 17 00:00:00 2001 From: Debopriyo Das <70844598+Dragon4926@users.noreply.github.com> Date: Fri, 7 Apr 2023 21:24:41 +0530 Subject: [PATCH] Added ***Arrow Function.md***file Recently, the mass popularity and use case of arrow functions led me to create and raise a PR of this file. IMPORTANCE: Arrow functions in JavaScript are concise and provide a shorter syntax for writing functions than traditional function expressions. They also have a linguistic binding, which means that the value of this inside an arrow function is based on the enclosing context and not on how the function is called. --- functions/Arrow Function.md | 146 ++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 functions/Arrow Function.md diff --git a/functions/Arrow Function.md b/functions/Arrow Function.md new file mode 100644 index 00000000..68854ced --- /dev/null +++ b/functions/Arrow Function.md @@ -0,0 +1,146 @@ + + +
+ + + + +Arrow functions
+ Arrow functions are a shorthand way to write functions in JavaScript. They + were introduced in ES6 and are often used in modern JavaScript code. +
+const greeting = () => {
+ console.log("Hello, world!");
+}
+greeting();
+ + In this example, greeting is an arrow function that takes no arguments and + logs the message "Hello, world!" to the console. The function is then + called with greeting(). +
+Arrow function syntax
+ Arrow functions have a concise syntax that can make them easier to read + and write. They use the => operator instead of the function keyword. +
+const add = (a, b) => a + b;
+console.log(add(2, 3));
+ + In this example, add is an arrow function that takes two arguments, a and + b, and returns their sum. The function is then called with add(2, 3) and + logs the result, 5, to the console. +
+Arrow functions and this
+ Arrow functions have a different behavior for the this keyword compared to + regular functions. When used inside an object method, arrow functions + preserve the this context of the surrounding scope. +
+const person = {
+ name: "Alice",
+ greet: function() {
+ console.log(`Hello, my name is ${this.name}.`);
+ },
+greetArrow: () => {
+ console.log(`Hello, my name is ${this.name}.`);
+ }
+ }
+person.greet(); // "Hello, my name is Alice."
+person.greetArrow(); // "Hello, my name is undefined."
+ + In this example, person is an object with two methods, greet and + greetArrow. The greet method uses a regular function and correctly logs + the name property of the person object. The greetArrow method uses an + arrow function and logs undefined because the this keyword refers to the + global this value, which does not have a name property. +
+Implicit return in arrow functions
+ Arrow functions with a single expression can have an implicit return + statement, meaning that the return keyword is not required. +
+const double = (num) => num * 2;
+console.log(double(5)); // 10
+ + In this example, double is an arrow function that takes a num argument and + returns its value multiplied by 2. The function is called with double(5) + and logs the result, 10, to the console. +
+Arrow functions and higher-order functions
+ Arrow functions can be used with higher-order functions, such as map, + filter, and reduce, to perform complex operations on arrays. +
+const numbers = [1, 2, 3, 4, 5];
+const doubled = numbers.map((num) => num * 2);
+
+console.log(doubled); // [2, 4, 6, 8, 10]
+ + In this example, numbers is an array of numbers. The map method is used + with an arrow function to create a new array doubled that contains each + number in numbers multiplied by 2. The result, [2, 4, 6, 8, 10], is logged + to the console. +
+Arrow functions with default parameter values
+ Arrow functions can use default parameter values, which are used when an + argument is not provided or is undefined. +
+const greet = (name = "world") => `Hello, ${name}!`;
+console.log(greet()); // "Hello, world!"
+console.log(greet("Alice")); // "Hello, Alice!"
+ + In this example, greet is an arrow function that takes a name argument + with a default value of "world". When called without an argument, the + default value is used. When called with the argument "Alice", that value + is used instead. +
+Arrow functions and the spread operator
+ Arrow functions can use the spread operator (...) to pass multiple + arguments as an array. +
+const add = (...nums) => nums.reduce((total, num) => total + num, 0);
+console.log(add(1, 2, 3)); // 6
+ + In this example, add is an arrow function that uses the spread operator to + accept any number of arguments as an array. The reduce method is used with + an arrow function to add up all the values in the array. The result, 6, is + logged to the console. +
+Arrow functions and object literals
+ Arrow functions can be used inside object literals to create methods with + concise syntax. +
+const person = {
+ name: "Alice",
+ greet() {
+ console.log(`Hello, my name is ${this.name}.`);
+ },
+ sayHi: () => {
+ console.log("Hi!");
+ }
+};
+
+person.greet(); // "Hello, my name is Alice."
+person.sayHi(); // "Hi!"
+ + In this example, person is an object with two methods, greet and sayHi. + The greet method uses a regular function and correctly logs the name + property of the person object. The sayHi method uses an arrow function and + logs the message "Hi!" without using this. Note that arrow functions are + not ideal for object methods that require this to refer to the object + itself. +
+ +