🔥 NEW Code Tidbit Every Week 🔥

# Royal Wedding with Default Parameter

Congratulations to the Duke and Duchess of Sussex!

Will you take Harry to be your husband? I know the default is “I will”. Luckily with ES6, we can set a default parameter for the traditionalists😉. But for me, I would respond… "HECK YEAH 💋" !!! 😆

function marryMe(yes = 'I will') {

marryMe(); // I will

marryMe('HECK YEAH 💋'); // HECK YEAH 💋

# Multiple Default Parameters

You can also have multiple parameters with default values.

function marryMe(yes = 'I will', prince = 'Harry') {

// yes
// Harry

# Single String console.log Output

You can print the the above line in one console.log statement. Just separate the value by a comma ,

function marryMe(yes = 'I will', prince = 'Harry') {
  console.log(yes, prince);

marryMe(); // I will Harry

# Skip Parameter with undefined

If you want to skip over parameters and maintain the default values. You can pass undefined as the argument for the parameters you want to gloss over.

function marryMe(yes = 'I will', prince = 'Harry', title = 'Sussex') {
  console.log(yes, prince, title);

marryMe(undefined, undefined, 'Cambridge'); // I will Harry Cambridge

# Community Examples

Multiple Default Parameters

You can print all the default params in an object.

const person = (age = 29, gender = 'male', location = 'NC') => {
  console.log({ age, gender, location });

// { age: 29, gender: 'male', location: 'NC' }

Thanks: Brian R.

☝️ The reason the above works is because it is utilizing ES6 enhanced object literal property value shorthand. When the property key matches the variable name, you can omit the variable name.

// Old Way
  age: age,
  gender: gender,
  location: location,

// Using ES6 object literal property value shorthand
  age, //: age,
  gender, //: gender,
  location, //: location

Thanks: Emmanuel K.

# Resources

Related Tidbits