Edson Edson - 1 year ago 75
Javascript Question

Executing a method inside a method

I'm currently working on a JavaScript exercise in FreeCodeCamp, and one of the test-cases my code should work with is a function call that looks like this:


here is the bare-bones function I'm given:

function addTogether() {

When I run the code below:

function addTogether() {
return arguments;

In the console, that the editor provides, I get:

TypeError: addTogether(...) is not a function

The instructions hint at using the
object, and it works well with test-case function calls that only have one argument object (i.e.
addTogether(2, 3);
), but not with the one I've shown above.

Is there a way to access/utilize the separate argument objects when they're in the format I showed above?

Note: I don't want any sort of answer to solve the problem, just info on any techniques on accessing the arguments of these type of function calls.

Help, is greatly appreciated.

Answer Source

Don't think of it as two separate sets of arguments. Think of it as you're calling another function (which you are). Functions are first-class values in JavaScript so you can use them just like any other value. This includes returning them from functions.

var f = function(y) {
  console.log('f:', y);

var getF = function(x) {
  console.log('getF:', x);
  return f;


Functions can also use values that exist in any parent scope. Loosely speaking, functions which do this are called closures.

function createGetX(x) {
  return function() {
    // Since x is in the parent scope, we can use it here
    return x;

var one = createGetX(1);
console.log(one()); // Always
console.log(one()); // returns
console.log(one()); // one

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download