1

I have the following function

//simple function with parameters and variable
        function thirdfunction(a,b,c,d){
            console.log("the value of a is: " + a);
            console.log("the value of b is: " + b);
            console.log("the value of c is: " + c);
            console.log("the value of d is: " + d);
            console.log("the arguments for each values are: " + arguments);
            console.log("the number of arguments passed are: " + arguments.length);
        }

        console.log("no parameter values are passed");
        thirdfunction();

        console.log("only a and b parameter values are passed");
        thirdfunction(1,2);

However when the values passed in arguments are not displayed if I concatenate the text the arguments for each values are:. Why is that?

The output I have from Google's console is as follows when concatenating;

no parameter values are passed
the value of a is: undefined
the value of b is: undefined
the value of c is: undefined
the value of d is: undefined
the arguments for each values are: [object Arguments]
the number of arguments passed are: 0
only a and b parameter values are passed
the value of a is: 1
the value of b is: 2
the value of c is: undefined
the value of d is: undefined
the arguments for each values are: [object Arguments]
the number of arguments passed are: 2 

The following values are passed when I don't concatenate.

no parameter values are passed
the value of a is: undefined
the value of b is: undefined
the value of c is: undefined
the value of d is: undefined
[]
the number of arguments passed are: 0
only a and b parameter values are passed
the value of a is: 1
the value of b is: 2
the value of c is: undefined
the value of d is: undefined
[1, 2]
the number of arguments passed are: 2 

EDIT

Not sure why the question got down voted but the problem I have is that when I use the statement console.log("the arguments for each values are: " + arguments); the output in the console is console.log("the arguments for each values are: " + arguments); however if I pass the statement console.log(arguments); the output in the console is [] or [1, 2]?

13
  • 2
    I don't understand the problem... Commented Jun 19, 2012 at 21:46
  • I don't fully understand your question. Where is the arguments variable set? Also the arguments passed to the function will default to undefined if not set. Commented Jun 19, 2012 at 21:46
  • @hsmoore arguments does not need to be set, it is available in all functions, it is an array representing all arguments called with the function. Commented Jun 19, 2012 at 21:49
  • @hsmoore.com - As per my example if I do console.log("the arguments for each values are: " + arguments); the response returned is [object Arguments] whilst if I do ` console.log("the arguments for each values are: " + arguments);` I get [] or [1, 2] Commented Jun 19, 2012 at 21:50
  • @PeanutsMonkey - A note here, arguments is not an array, that's why it only shows [object Arguments]. Commented Jun 19, 2012 at 21:52

1 Answer 1

5

Writing console.log("..." + arguments) it will force conversion of arguments into string. Since arguments is an object, its string representation is [object Arguments]. If you instead want to display contents of that object, try passing it without concatenation:

console.log("the arguments for each values are: ", arguments);
Sign up to request clarification or add additional context in comments.

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.