ES6 Tutorial - Default parameters
   Dec. 29th,2017
   Ismail Nassor Mbarack

If you're familiar with some backend languages like PHP you won't be surprised to hear that functions parameters have default values, yes default values. Unlike in ES5, functions in ES6 can be given default values during declaration and we don't have to do so in the function.Let's take a look at the following example:-

 

let cart = {
    getPrice(price,discount){
        //this is what we should have done in the past to set a default value
        let disc = discount || 0.1;
        return price - (price * disc);
    }
}

let anotherCart = {
    //now we can simply add a default value like this
    getPrice(price,discount = 0.1){
        return price - (price * discount);
    }
}

console.log('=====default value in ES5=======');
console.log('Total:'+cart.getPrice(100));
console.log('Total:'+cart.getPrice(100,0.2));

console.log('=====default value in ES6=======');
console.log('Total:'+anotherCart.getPrice(100));
console.log('Total:'+anotherCart.getPrice(100,0.2));


 

 

Quite a simple feature but very useful at times.Happy coding....