1//If body has single statement
2let myFunction = (arg1, arg2, ...argN) => expression
3
4//for multiple statement
5let myFunction = (arg1, arg2, ...argN) => {
6 statement(s)
7}
8//example
9let hello = (arg1,arg2) => "Hello " + arg1 + " Welcome To "+ arg2;
10console.log(hello("User","Grepper"))
11//Start checking js code on chrome inspect option
1/* Answer to: "arrow function javascript" */
2
3// Single-line:
4let testingFunc(string) => string == "Test" ? "Success!" : "Failure!";
5console.log(testingFunc("test")); // "Failure!"
6
7// Multi-line:
8let arrowFunc(string) => {
9 if (string = "test") {
10 return "Success!";
11 }
12 return "Failure!";
13 }
14};
15console.log(testingFunc("Test")); // "Success!"
16
17/*
18 Arrow functions in JavaScript are like regular functions except they look
19 look nicer (imo) and there's single-line version of it which implicitly
20 returns.
21
22 Here's a guide showing the differences between the two:
23 https://medium.com/better-programming/difference-between-regular-functions-and-arrow-functions-f65639aba256
24 > The link will also be in the source below.
25*/
1// an arrow function is also called a lambda or an anonymous function
2
3let myFunction = () => {
4 // some logic
5}
1// Traditional Function
2function (a, b){
3 let chuck = 42;
4 return a + b + chuck;
5}
6
7// Arrow Function
8(a, b) => {
9 let chuck = 42;
10 return a + b + chuck;
11}
1hello4 = (name) => { return ("Hello " + name); }
2 //OR
3hello5 = (name) => { return (`Hello new ${name}`) }
4
5
6document.getElementById("arrow").innerHTML = hello4("arrow function");
7
8document.write("<br>" + hello5("arrow function"));
1// Traditional Function
2function (param) {
3 var a = param * 3;
4 return a;
5}
6
7//Arrow Function
8(a, b) => {
9 let c = (a * b) + 3;
10 return c;
11}
1([a, b] = [10, 20]) => a + b; // result is 30
2({ a, b } = { a: 10, b: 20 }) => a + b; // result is 30
3
1const power = (base, exponent) => {
2 let result = 1;
3 for (let count = 0; count < exponent; count++) {
4 result *= base;
5 }
6 return result;
7};
8
9//if the function got only one parameter
10
11const square1 = (x) => { return x * x; };
12const square2 = x => x * x;
13
14// empty parameter
15
16const horn = () => {
17 console.log("Toot");
18};