3

Let it be so :

let something = (a , b ,  c = 0 ,  d = 0 ,  e = 0) =>
{console.log(`a is ${a}; b is ${b};c is ${c};d is ${d};e is ${e}`)};`

If I give more then two params to a function, it takes them as respectively. What if I want to pass the value to "e" variable and skip others, that are predefined?

2 Answers 2

1

You can make the params an object.There are two benefits to it, first, you don't have to worry about the order and you won't have to pass a value to c or d.

let something = ({a , b ,  c = 0 ,  d = 0 ,  e = 0}) => {
  return `a is ${a}; b is ${b};c is ${c};d is ${d};e is ${e}`;
}
console.log(something({a: 12 , b: 13, e: 51})); //"a is 12; b is 13;c is 0;d is 0;e is 51"
Sign up to request clarification or add additional context in comments.

Comments

1

Just pass undefined:

let something = (a, b, c = 0, d = 0, e = 0) => {
  console.log(`a is ${a}; b is ${b}; c is ${c}; d is ${d}; e is ${e}`);
};
something(1, 2, undefined, undefined, 3);

You can also wrap the optional parameters in a single argument which will be destructured:

let something = (a, b, {c=0, d=0, e=0} = {}) => {
  console.log(`a is ${a}; b is ${b}; c is ${c}; d is ${d}; e is ${e}`);
};
something(1, 2);
something(1, 2, {c: 3});
something(1, 2, {e: 3});

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.