Java Reference
In-Depth Information
invoke the
mean
function with only two arguments, we can see that it returns
NaN
because
the function cannot do the required operation with
undefined
:
mean(1,2)
<< NaN
If too many arguments are provided when a function is invoked, the function will work as
normal and the extra arguments will be ignored (although they can be accessed using the
arguments object that is discussed in the next section):
mean(1,2,3,4,5); // will only find the mean of 1,2 and 3
<< 2
The
arguments
Variable
Every function has a special variable called
arguments
. This is an array-like object that
contains every argument of the function when it is invoked:
function arguments(){
return arguments;
}
Warning:
arguments
is not an Array!
Be careful:
arguments
is not an array. It has a
length
property and you
can read and write each element using index notation, but it doesn't have
array methods such as
slice()
,
join()
, and
forEach()
. (However,
there is a way of "borrowing" these methods from arrays that we will look
at in a later chapter.)
Warning: Rounding Errors
The last example highlights a problem when doing division in JavaScript.
Because it uses base 2 in the background, it can struggle with some division
calculations and often has slight rounding errors. This usually doesn't cause
a problem, but you should be aware of it.
