1

I know its silly to ask this question but still, I want to know why don't javascript throw an error if we declare a function with only 1 parameter and pass multiple arguments.

function checkIt(a){
return a;
}
checkIt(6,5);

and the answer we got is 6.

3 Answers3

3

It's just how JS is implemented-- you can have less or more arguments without any errors.

If you have less, you get undefined for where you didn't enter any arguments.

If you have more, it's simply put into the arguments array. Usually, if you want to accept a misc. number of arguments, you'll use the (...) operator in parameters to let people know that it's a function that accepts an unknown number of arguments. Also, the good thing about the (...) operator is that it only grabs arguments that aren't known, as opposed to the arguments variable which just grabs everything.

TedTran2019
  • 887
  • 5
  • 15
1

Interesting question.

JavaScript is a "Loosely Typed" Language.

In programming, we call a language "loosely typed" when you don't have to explicitly specify types of variables and objects.

JavaScript is loosely typed. You don't have to tell that a string is a string, nor you can require a function to accept an integer as its parameter.

This gives JavaScript a lot of flexibility. Flexibility lets you move faster, change things quickly, iterate at a faster velocity.

One of the features of loosely typed language is that they auto initialize and typecast variables as required. Hence, when the JS interpreter reads the arguments and sees that one of the argument is missing, it automatically initializes it as undefined and continues with the further programming flow.

This is a concept which almost applicable to all loosely typed languages.

Thanks.

entreprenerds
  • 957
  • 10
  • 20
  • 1
    Which other loosely type languages behave that way? I'm asking because I'm doubting that this is true. I can totally see how *only* loosely typed languages behave this way, but I still doubt that it's common among such languages. Python doesn't behave this way for example. – Felix Kling Nov 04 '19 at 06:49
  • Yes I agree, it is not a common behaviour amongst other LTLs like python, but as a point of principal, python or rather any language other than JS is not purely a LTL, is it? – Gobind Deep Singh Nov 04 '19 at 07:00
  • @FelixKling: because python is not loosely-typed? PHP is interesting - it doesn't allow to pass too few args, but too many are fine. – georg Nov 04 '19 at 07:34
  • @georg: Why is it not loosely typed? Because there is no type coercion when using an operator on the wrong type? – Felix Kling Nov 04 '19 at 08:20
  • @georg I see that I’ve been confusing this with something else. You can assign a value of a different data type to a variable, but that doesn’t means it’s weakly typed. – Felix Kling Nov 04 '19 at 08:29
  • @FelixKling: yes, these terms are vague somehow, python is "strong" with operators ("cannot add int and str"), but functions are "typed" only by their arity, not argument types. – georg Nov 04 '19 at 08:38
0

One reason is that it's not necessarily possible to determine how many arguments a function should accept, just from reading it. For example, what if your checkIt checked arguments[1] somewhere? (But that's not something for the interpreter to scan for on function invocation)

function checkIt(a) {
  console.log(arguments[1]);
  return a;
}
checkIt(6, 5);

Or, the same thing with argument rest syntax:

function checkIt(...args) {
  console.log(args[1]);
  return args[0];
}
checkIt(6, 5);

The argument index to find could even be dynamic:

function checkIt(argIndex, ...args) {
  console.log(args[argIndex]);
}
checkIt(1, 5, 6, 7);

Passing more arguments than stated in the parameter list may well be a code smell, but just like missing semicolons and unnecessary semicolons, there's no explicit reason for it to throw an error, so it doesn't.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320