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?

1
  • 1
    You could pass through undefined Commented Apr 2, 2022 at 4:18

2 Answers 2

0

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

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

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

Comments