11let a = ( ) => {
22 // This is arrow function came new in ES6
3+ //It assigns the function to variable a as the identifier with let instead and adds arrows before the curly braces.
4+
35} ;
46
7+
8+ let multiply = ( num ) => num * num
9+
10+ //arrow functions also works without curly braces {} and can directly write expression after the arrows
11+ // this is known as concise body as opposed to a block body (with {});
12+ //cannot be used with if statements, or an error will appear since it only takes one expression;
13+ //ternary operators can be used with arrow functions as a more concise way to write if statements
14+
15+
16+
517let info = {
618 firstName : "Swapnil" ,
719 lastName : "Shinde" ,
820 getFullName : ( ) => {
9- return ( `My name is ${ this . firstName } ${ this . lastName } ` ) ; // Arrow functions don't have "this" property
21+ return ( `My name is ${ this . firstName } ${ this . lastName } ` ) ; // Arrow functions don't have "this" property
1022 }
1123}
24+ //not having this. binding means it also cannot be called with new and used as a constructor
25+
1226
1327console . log ( info . getFullName ( ) ) ;
1428// Output My name is undefined undefined that's why we don't use this with arrow function
@@ -17,7 +31,7 @@ let newInfo = {
1731 firstName : "Swapnil" ,
1832 lastName : "Shinde" ,
1933 getFullName : ( ) => {
20- return ( `My name is ${ newInfo . firstName } ${ newInfo . lastName } ` ) ; // If we are using arrow function then directly use the variables as shown
34+ return ( `My name is ${ newInfo . firstName } ${ newInfo . lastName } ` ) ; // If we are using arrow function then directly use the variables as shown
2135 }
2236}
2337
@@ -31,32 +45,42 @@ class Student {
3145 }
3246
3347 getName = ( ) => {
34- return this . name ;
48+ return this . name ;
3549 }
3650}
3751
3852console . log ( ( new Student ) . getName ( ) ) // Gives error for node versions before 12.4.0(Approx) SyntaxError: Unexpected token =
3953
4054class StudentInfo {
4155
42- constructor ( firstName , lastName , age , branch , college ) {
43- this . firstName = firstName ;
56+ constructor ( firstName , lastName , age , branch , college ) {
57+ this . firstName = firstName ;
4458 this . lastName = lastName ;
45- this . age = age ;
59+ this . age = age ;
4660 this . branch = branch ;
4761 this . college = college ;
4862 } ;
4963
5064 getFullName = ( ) => { // Returns full name using string interpolation
51- return ( `My name is ${ this . firstName } ${ this . lastName } ` ) ; // If we are using arrow function then directly use the variables as shown
65+ return ( `My name is ${ this . firstName } ${ this . lastName } ` ) ; // If we are using arrow function then directly use the variables as shown
5266 } ;
5367
5468 getBranch = ( ) => { // Returns Branch
55- return ( this . branch ) ;
69+ return ( this . branch ) ;
5670 } ;
5771
5872}
5973
60- let Swapnil = new StudentInfo ( "Swapnil" , "Shinde" , 19 , "Computer" , "Sies" ) ; // This way we can create new objects with arguments
74+ let Swapnil = new StudentInfo ( "Swapnil" , "Shinde" , 19 , "Computer" , "Sies" ) ; // This way we can create new objects with arguments
75+
76+ console . log ( Swapnil . getFullName ( ) ) ; // Output My name is Swapnil Shinde
77+
78+ //settimeout without arrow function
79+ setTimeout ( function ( ) {
80+ console . log ( "hello world" ) ;
81+ } , 1000 ) ;
6182
62- console . log ( Swapnil . getFullName ( ) ) ; // Output My name is Swapnil Shinde
83+ //settimeout with arrow function
84+ setTimeout ( ( ) => {
85+ console . log ( "hello world" ) ;
86+ } , 0 ) ; //arrow functions provide better readability
0 commit comments