Return value of a function for Arrays

Hello Everyone,

I’m learning JavaScript nowadays and have a query. Consider a function with following declaration.

function args1()
	var a = [1,2,3];
	return a;

When I try to call above function as below, return type is array

alert (typeof args1(1,2,3)); //array
alert (args1(1,2,3)); //1,2,3

But this is not true for local array variable “arguments” which JavaScript creates

function args2()
	return arguments;

alert (typeof args2(1,2,3)); //object
alert (args2(1,2,3)); //[object][object]

In both situations, function is expected to return array values. It is an array in first case but object in second case. Could anyone please explain on this? Why it is so?

The arguments object is an array-like object, but it is not an actual array. It cannot be just an array because changing a value in the arguments object also changes the corresponsing function parameter as well. You can convert the arguments object to an array though, if you require the information in that data format.