0

I want to supply a function with only some of its parameters, and let the rest revert to their defaults. For example, I have a function like this:

function doSomething(a = 'apple', b, c){
    //Do something with a, b and c
}

Now I want to call this function, but only supply arguments b and c (so that 'a' defaults to 'apple'). What is the best way to do this?

4 Answers 4

2

Normally, the defaults should be at the very end of your function (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters):

function doSomething(b, c, a = 'apple'){
    //Do something with a, b and c
}
Sign up to request clarification or add additional context in comments.

1 Comment

I read somewhere that you can pass 'undefined' as a placeholder, instead of reordering default parameters to the end. Is that bad practice?
1

There is no absolute best way of doing it. But, there are few ways that can simplify and bring in more meaning to the code written with default parameters.

You can define all the default parameter in the starting of the arguments and for rest of the parameters which doesn't have a default value, make use of the rest operator.

Eg -

function doSomething(a='apple',b='banana',...args){
    console.log(a);
    console.log(b);
    console.log(args);
}

let x,y;
doSomething(x,y,"hello","hey","hi");

output -

enter image description here

Go through the following link to get more understanding of the rest operator.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters

Though a little unrelated. By default javascript, parameters are undefined if they are not passed any value or reference. You can check that the parameters are not undefined inside your code block.

Comments

1

A useful pattern (used widely in the JavaScript world) is to allow callers to pass an options object, if entries are missing they will fall back to default values.

It's particularly useful if you're dealing with a lot of parameters.

function doSomething(a, b, options) {
    // Specify your defaults here...
    let defaults = { c: 3, d: "bar" };
    options = Object.assign({}, defaults, options);
    let { c, d } = options;
    console.log("doSomething: (a,b,c,d):", a, b, c, d);
}

doSomething(1,2, { c: 5, d: "foo" });
doSomething(1,2);

2 Comments

Ok, that's useful. So just to be clear, the Object.assign will first give the default values to options variable, and then, if there are options passed by the user, it will overwrite them with those?
Yes, that's right! The Object.assign function will overwrite any default property with the same property from options passed by the user if present. One could also use the spread syntax, e.g. {...defaults, ...options}
0

hope this will be clear if value of a param should always be same :

function doSomething(b, c,  a = 'apple'){
    console.log({b:b},{c:c},{a:a})
}

doSomething('hi','hello','a is not apple here--')

function doSomething1(b, c, a){
    a = 'apple'
    console.log({b:b},{c:c},{a:a})
}

doSomething1('hi','hello','a is apple here--')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.