1

I want to be able to pass in any number of arguments into a function and then be able to use those arguments later. I know I can pass in default arguments but I don't know what to declare in my function.

For example,

function test(????) {
  console.log(a)
  console.log(b)
  console.log(c)
}

test(a="a", b="b", c="c")

I'm also challenged by being given this function and to make as little changes to the var declarations as much as possible. So here, variables, a, b, and c are declared and I'm not able to edit these variables. I should be able to pass in arguments that will then know to assign itself to these variables.

function test(???) {
  var a,b,c
  if (a>b){
    c = a + b
  }
}

test(a=2,b=3)
jamesvphan
  • 1,825
  • 6
  • 23
  • 30

6 Answers6

4

You actually don't need to define any arguments in the function parameters. All you need to do is access javascript's built in arguments object.

So your code could look like the following:

function test() {
    var a = arguments[0];
    var b = arguments[1];
    var c = arguments[2];

    console.log(a);
    console.log(b);
    console.log(c);
}

test("a", "b", "c");

For reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments

Daniel W.
  • 555
  • 3
  • 8
3

Using an array is a good idea, but in the interest of completeness...

The ES6 way!

If you're able to support ES6 features, the spread operator combined with the arguments keyword is a neat way to get around that:

function anyNumber() {
  console.log(...arguments); // ->  1 2 3 4

  let argsAsArray = [0, ...arguments, 5]; // now they're an array

  argsAsArray.forEach(s => console.log(s)); // -> 0 1 2 3 4 5
};

anyNumber(1,2,3,4);

There are a lot of cool things you can do with the spread operator, especially with object and parameter destructuring.

joh04667
  • 7,159
  • 27
  • 34
1

you could pass in an object or an array:

function test1([a, b, c]) {
   // ...
}
test1([1, 2, 3]);

function test2({ a, b, c }) {
   // ...
}
test2({ a: 1, b: 2, c: 3 });
kindoflike
  • 437
  • 4
  • 16
1
function test(input) {
  console.log(input.a);
  console.log(input.b);
  console.log(input.c);
}

test({ a: 1, b: 2, c: 3 });
Ram
  • 504
  • 3
  • 11
0

You should use an array or an object.

In that array add as many arguments you want.

function test(arr) {
   console.log(arr.a);
   console.log(arr.b);
   console.log(arr.c);
}

arr = {}
   
arr.a = "a";
arr.b = "b";
arr.c = "c";
    
k = test(arr);
    
    
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
  • This is what I was thinking, except I'm working with a function where the variables are pre-set, meaning I can't change the var declarations. I have to make sure what I pass into the function, my arguments will need to connect with their corresponding declarations inside the function. – jamesvphan Jun 07 '17 at 21:33
  • @jamesvphan That's not a problem just assign those to a newly created object. – Pritam Banerjee Jun 07 '17 at 21:34
0

Given javascript at updated Question, you can define default parameters within () following function name.

function test(a = 2, b = 3) {
  let c;
  if (a > b) {
    c = a + b
  } else {
    c = 0
  }
  console.log(c);
}

test(); // 0

test(4); // 7

See also Can we set persistent default parameters which remain set until explicitly changed?

guest271314
  • 1
  • 15
  • 104
  • 177
  • I think this is what I need, but the code snippet is giving me an error from the fn(...opts) function you had – jamesvphan Jun 07 '17 at 21:45
  • @jamesvphan The error was probably due to use of object rest `{...opts.map((prop, index)}`. Object rest `{a, b, ...rest}` is available using `--javascript-harmony` flag at chromium, chrome; or using `babel` standalone. Will enable `babel` at stacksnippets – guest271314 Jun 07 '17 at 21:48
  • @jamesvphan See updated stacksnippets – guest271314 Jun 07 '17 at 21:53