What happens if I call a JS method with more parameters than it is defined to accept?

JavaScript doesn’t have the concept of a fixed parameter list. For your own functions you can always specify as many parameters as you want and pass in as many as you want which ever type you want.

For built-in functions, which correlate to native code, it depends.

You asked on what it depends:

Let’s look at the ECMA-262

Section 15 about built-in (not to confuse with host) functions in general

Unless otherwise specified in the description of a particular function, if a function or constructor described in this clause is given fewer arguments than the function is specified to require, the function or constructor shall behave exactly as if it had been given sufficient additional arguments, each such argument being the undefined value.

Alright. If I pass in less arguments than needed, it depends on the spec of the function itself (scroll down section 15 to find the spec for each built-in function).

Unless otherwise specified in the description of a particular function, if a function or constructor described in this clause is given more arguments than the function is specified to allow, the extra arguments are evaluated by the call and then ignored by the function. However, an implementation may define implementation specific behaviour relating to such arguments as long as the behaviour is not the throwing of a TypeError exception that is predicated simply on the presence of an extra argument.

Passing in too many arguments should never raise a TypeError. But still it may raise other errors. Again, it depends on the function you talk about.

You were talking explicitly about the DOM and not about built-in functions. To be honest I can’t find the corresponding parts of the spec. The ECMA spec is so much easier to read then the w3 website.

Leave a Comment