8

I have a javascript function that takes an object as a parameter like this:

const someFunc = ({ a }) => { <do something> }

I call the function like this:

a = 'some value'
someFunc({ a })

But sometimes, I need to call the function without passing a. In that case, I need to use a default value for a. How can I add a default value for a key inside an object?

1

4 Answers 4

14

I think you're looking for default parameters

const someFunc = ({ a = "foo" }) => {
   console.log(a);
}
someFunc({}); // "foo"
someFunc({a: "bar"}); // "bar"

Update If you also want to have a as default to "foo" without passing any argument, you need to set a default parameter also for the object that contains a. Something like:

const someFunc = ({ a = "foo" } = {}) => {
   console.log(a);
}
someFunc(); // "foo"

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

4 Comments

Your code doesn't run : Uncaught TypeError: Cannot read property 'a' of undefined
Well, you're edit didn't work, my code was working assuming what the OP asked (that "a" wasn't passed, not the object that contains "a"). Your edit should have added any object to the someFunc() call. If the OP want to call someFunc also without passing anything, the function signature should include that.
I didn't edit anything. I made a snippet and called your own function. Your edit should have added any object to the someFunc() call No, that's precisely the point, OP wants a default parameter if none is passed, so...
You add the function call in a way wasn't intended to be called: of course it doesn't work. This is "editing the (running) code", you didn't merely add the snippet. Any code won't work if called improperly. The OP, literally, wrote: "I need to call the function without passing *a*". And a is the property, not the object. And that I replied to. If he meant the whole object, I edited my answer to cover that scenario too. But it's not what he wrote.
3

const someFunc = ({a, b, c ,d} = {a:10, b: 12, c:3, d:4}) => {
   console.log(a, b, c ,d);
}
someFunc()

Remember that this code wont actually work in IE.

Here is the workaround for IE:

    var someFunc = function someFunc() {
    var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
       a: 10
    },
    a = _ref.a;

    //here starts the function
    console.log(a);
};
someFunc();

2 Comments

That works! Also, what if I want to call it like someFunc({})? This might be required if I have multiple keys inside the object.
Unfortunately, that will not work if I call it like someFunc({}). Here I am passing an empty object instead of nothing.
3

ES6 accepts default values for parameters :

const someFunc = ({a} = {a : 6}) => { 
  console.log({a})
}
someFunc({ a : 3 })
someFunc()

2 Comments

That works! Also, what if I want to call it like someFunc({})? This might be required if I have multiple keys inside the object.
JS is loosely typed, you can pass any object to the function if it's defined like this : someFunc = (obj = {a : 6}). If the answer worked for you, please don't forget to mark it as accepted!
0

const someFunc = ({ a }) => { 
 typeof a === 'undefined' 
 ? a = 'some default'
 : a = a;
 console.log(a);
}

a = 'some value';
someFunc({ a });
someFunc({});

2 Comments

What if a is a Boolean and has value of false? It would be better to check if a is undefined.
This is an odd way to write a ternary assignment. Usually I'd do a = a === undefined ? "default" : a rather than (ab)use assignment expressions. Could you provide an explanation for why this is preferable to the existing answers?

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.