Welcome Guest! (App Version 3.1)

websitnero logo name

Default Parameters

Default Parameters in Functions

Another new features in ES6 (EcmaScript 2015) is Default Parameters. This means that if the number of parameters that a function asks are satisfied then its fine, but if the number of parameters falls less than those in the arguments of function, then you got an option of setting some default values for them.

5

Look at the following examples:

alpha = (a=10, b=5, c=2) => a * b -c; 

alpha(13, 5, 3); //result  is 62
alpha(8,3); //result is 22
alpha(7); // result is 33
alpha(9,,8); //Error: Unexpected token ,

Explaination: In first case when you are passing (13, 5, 3) then no default values will be picked in function as all values are passing so (13 * 5 -3) = 62. In second case you are passing first 2 values but third is not passed, so it will be picked as default. So final result is (8 * 3 -2 = 22). In third case, since only first value is passed so 2nd and 3rd values in arguments will be picked by default values. So, result is (7 * 5 -2 = 33). Also, you cannot skip values while passing to function parameters, so final function call will throw error.

WebsiteNero is highly optimized for learning various website developing technlogies. We try our best to add maximum modules and examples to help learn the concepts clearly and vividly. We try to present all content and examples as simple as we can removing any complexity to hurdle easy understanding. We try best provide you with worthful content, but we cannot guarantee full correctness of all the content on this site. While using this website, you agree to have read and accepted all our terms of use and conditions, cookie, and privacy policy. Copyright, 2013-2018 by websitenero.com. All Rights Reserved.