2

I want to pass a parameter to override a deafult parameter that comes later in the function declaration than another default parameter that I want to let be default. Is this possible? Here's an example of what I want to do:

function f(a:string, b:number=1, c:string='FOO'){ // do stuff } f('val for a', 'val for c') // want b to still be default of 1 
1
  • Please accept answers to your questions that you found to be satisfactory by clicking the checkmark outline next to them. Commented Oct 9, 2024 at 22:02

2 Answers 2

5

According to documentation you need to explicitly set b to undefined to get the default.

function test(a = 1, b = 2, c = 3) { console.log(a, b, c); } test(undefined, 42, undefined);

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

1 Comment

sounds like it cannot be done and that I have to do this. Thanks. Solved
2

You can use undefined to get the default working. Here's the way you can accomplish this thing in a good stylistic way

function test(a:string, b:number=1, c:string='FOO') { //other stuff } let _ = undefined test('val for a', _, 'val for c'); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.