ES6 Syntax Examples II : Arrow (Fat Arrow) Functions & Function Params

Hello,

This blog is about arrow functions & default function parameters on ES6. I'm gonna put some small ES6 code pieces which I found interesting & different from ES5. I hope you find it useful & helpful.

Single Expression Return
'use strict';  
var getPrice = (count,tax) => count*4.00*(1+tax);  
console.log(getPrice(2,0.7));  

Logs: 8.56


Spesifing a Block
'use strict';  
var getPrice = (count,tax) => {  
    var price = count*4.00;
    price *= (1+tax);
    return price;
}
console.log(getPrice(2,0.7));  

Logs: 8.56


this Keyword within Functions in ES5
'use strict';  
document.addEventListener('click',function(){  
    console.log(this);
});

Logs: #document


this Keyword within Functions in ES6
'use strict';  
document.addEventListener('click',()=>console.log(this));  

Logs: Window {...}


Adding New Line to Inline of Arrow Function Gives Syntax Error
'use strict';  
var getId = ()  
    => 10;
console.log(typeof getId);  

Logs: SyntaxError: Unexpected token =>


Prototype Property ?

Normally when we declare a function in js it'll have a prototype property.

'use strict';  
var getId = () => 10;  
console.log(getId.hasOwnProperty('prototype'));  

Logs: false


Default Parameters in ES6
'use strict';  
var getBook = function(id=50,type='cook'){  
    console.log(id+' ,'+type);
}
getBook(undefined,'programming');  

Logs: 50, programming

If you spesify a 'undefined' as parameter in ES6, it'll get the default value of that parameter.


Another Example of Default Parameter in ES6
'use strict';  
var baseTax = 0.07;  
var getTotal = function(price,tax=price*baseTax){  
    console.log(price+tax)
}
getTotal(5.00);  

Logs: 5.35


2 Default Parameter
'use strict';  
var getTotal = function(price = adjustment,adjustment=1.00){  
    console.log(price+adjustment);
}
getTotal(5.00);  

Logs: 6

Feel free to add more on disquss so I can update. Thanks for reading.