简体   繁体   中英

Why does arguments in Javascript return [object Arguments] when using console.log?

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] ?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM