1+ // Path: src/01-intro/07-modern.js
2+
3+ /* Arrow function example */
4+ const circleAreaFn = function circleArea ( radius ) {
5+ const PI = 3.14 ;
6+ const area = PI * radius * radius ;
7+ return area ;
8+ } ;
9+ console . log ( circleAreaFn ( 2 ) ) ; // 12.56
10+
11+ // refactoring to use arrow function
12+ const circleArea = ( radius ) => { // {1}
13+ const PI = 3.14 ;
14+ return PI * radius * radius ;
15+ } ;
16+
17+ // simplified version
18+ const circleAreaSimp = radius => 3.14 * radius * radius ;
19+ console . log ( circleAreaSimp ( 2 ) ) ; // 12.56
20+
21+ // no parameters
22+ const hello = ( ) => console . log ( 'hello!' ) ;
23+ hello ( ) ; // hello!
24+
25+ /* Spread operator example */
26+ const sum = ( x , y , z ) => x + y + z ;
27+
28+ const numbers = [ 1 , 2 , 3 ] ;
29+ console . log ( sum ( ...numbers ) ) ; // 6
30+
31+ /* Rest operator example */
32+ const restParamaterFunction = ( x , y , ...a ) => ( x + y ) * a . length ;
33+ console . log ( restParamaterFunction ( 1 , 2 , 'hello' , true , 7 ) ) ; // 9
34+
35+ /* Exponentiation operator example */
36+ const r = 2 ;
37+ let area = 3.14 * r * r ;
38+ area = 3.14 * Math . pow ( r , 2 ) ;
39+ area = 3.14 * r ** 2 ;
40+
41+ // to see the output of this file use the command: node src/01-intro/07-modern.js
0 commit comments