1

I want to create a function with default params, but I want to use one default param and then use a custom value for the following one. For instance,

function greet(name, greeting = 'Hey', question = 'How are you?') {
    console.log(`${greeting}, ${name}! ${question}`);
  }

greet('Jack', , 'You good?'); // tried this, doesn't work :(

What if I wanted to say, "Hey, Jack! You good?", where I use the default for the first param with a default value, but not the second? I have tried calling the function with nothing between the commas in the function call. Is there a way to do this in JS without explicitly passing 'Hey', the defined default value, to the param that has a default already assigned?

2 Answers2

0

function greet(name, greeting = 'Hey', question = 'How are you?') {
    console.log(`${greeting}, ${name}! ${question}`);
  }

greet('Jack',undefined , 'You good?');
Hritik Sharma
  • 1,756
  • 7
  • 24
0

You can pass undefined for the second param. It should work as you expect it to be.

greet('Jack', undefined, 'You good?');