-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13. Create arrow function .js
More file actions
44 lines (37 loc) · 1.64 KB
/
13. Create arrow function .js
File metadata and controls
44 lines (37 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// create an arrow function :
// --------------------------------------------------------------------------------------------------------------::
// Basic arrow function - no parameters
// ------------------------------------------------------------------------
const greet = () => "Hello World!";
console.log(greet());
// Arrow function with parameters
// ------------------------------------------------------------------------
const add = (a, b) => a + b;
console.log(add(5, 3));
// Multi-line arrow function - requires return statement
// ------------------------------------------------------------------------
const calculate = (x, y) => {
const sum = x + y;
return sum * 2;
};
console.log(calculate(3, 4));
// Single parameter - no parentheses needed
// ------------------------------------------------------------------------
const square = x => x * x;
console.log(square(4));
// Arrow function returning object - use parentheses
// ------------------------------------------------------------------------
const createUser = (name, age) => ({ name, age });
console.log(createUser("Ayush", 22));
// Arrow function with array methods
// ------------------------------------------------------------------------
const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(n => n * 2); // transforms each element
const evens = numbers.filter(n => n % 2 === 0); // filters elements
console.log(doubled);
console.log(evens);
// Higher-order function - function returning function
// ------------------------------------------------------------------------
const multiply = (factor) => (num) => num * factor;
const triple = multiply(3);
console.log(triple(5));