function Add(a, b) { }
If we call a javascript function like – onclick="Add(1)"
Why do we don’t get error even though we pass only a single parameter or even more than 2 parameters (in case of above example) to function ?
How Java script treats above scenario?
Javascript is a dynamic, weakly-typed language. As a result, it doesn’t strictly enforce method signatures.
Addis a Function object. It has a property calledargumentswhich is an array-like object that contains the parameters that you pass in. As a convenience, it will also create local variables calledaandband assign the first and second elements inargumentsto them. In the case where you only have one input parameter,bwill be undefined.So, Javascript will treat
and
as almost identical. The difference here is that the
argumentsvariable will be of length 2 instead of 1. From a purely pragmatic standpoint, though, they’re pretty well the same.