Last active
November 12, 2018 14:12
-
-
Save emfluenceindia/cbb89527c47f148bdff74cd50e60c5a8 to your computer and use it in GitHub Desktop.
Default Parameters in ES6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Default parameter syntax in ES6 | |
// Until ES5 we were not able to declare a default value to function paramters | |
// ES6 gives us the liberty to do that | |
// Parameters value type has no restriction, it could either be an Integer or a String or an Array or even a JavaScript object | |
// ************************************************* | |
// THE ES5 WAY: Let's see how we use to do it in ES5 | |
// *************************************************/ | |
var objAboutPerson = { | |
location: { | |
city: 'Kansas City', | |
state: 'Missouri', | |
country: 'USA' | |
}, | |
gender: 'Male', | |
age: 47, | |
email: '[email protected]' | |
} | |
var arrHobby = [ | |
'Coding', | |
'Soccer', | |
'Travel', | |
'Music', | |
'Photography', | |
]; | |
function getUser(name, role, status, objAboutPerson, arrHobby) { | |
// Set default values for each parameter. | |
name = name || ''; | |
role = role || 'Subsriber'; | |
status = status || 'Pending'; | |
objAboutPerson = objAboutPerson || {}; | |
arrHobby = arrHobby || []; | |
console.log(name, role, status, objAboutPerson, arrHobby); | |
} | |
getUser( 'John Astor', 'Subsriber', 'Active', objAboutPerson, arrHobby ); | |
// **************************************************** | |
// THE ES6 WAY: Here is how we do it with ES6 | |
// ****************************************************/ | |
var objAboutPerson = { | |
location: { | |
city: 'Kansas City', | |
state: 'Missouri', | |
country: 'USA' | |
}, | |
gender: 'Male', | |
age: 47, | |
email: '[email protected]' | |
} | |
var arrHobby = [ | |
'Coding', | |
'Soccer', | |
'Travel', | |
'Music', | |
'Photography', | |
]; | |
function getUser(name = '', role = 'Subscriber', status = 'Pending', objPerson = {}, arrHobby = []) { | |
console.log(name, role, status, objAboutPerson, arrHobby); | |
} | |
getUser( 'John Astor', undefined, 'Active', objAbooutPerson, undefined); | |
// ***************************************************** | |
// WHAT IS UNDEFINED | |
// *****************************************************/ | |
// In ES6 since we have the ability to set a default value for each parameter, we also have the | |
// ability to "SKIP" one or more values when calling the function. | |
// We do this by passing 'undefined' (without quotes, as shown above). The keyword 'undefined' forces the funciton | |
// to use the default value of that parameter. | |
// It is needless to say however, the order of the values being passed must be the same the order parameters are | |
// declared in the function. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment