2

Why doesn't this function throw an error if the remaining arguments are missing?

showStatistics("Mark Teixeira", "New York Yankees", "1st Base");

Here is a the function defined:

function showStatistics(name, team, position, average, homeruns, rbi) {
  document.write("<p><strong>Name:</strong> " + arguments[0] + "<br />");
  document.write("<strong>Team:</strong> " + arguments[1] + "<br />");

  if (typeof arguments[2] === "string") {
    document.write("<strong>Position:</strong> " + position + "<br />"); 
  }
  if (typeof arguments[3] === "number") {
    document.write("<strong>Batting Average:</strong> " + average + "<br />");
  }
  if (typeof arguments[4] === "number") {
    document.write("<strong>Home Runs:</strong> " + homeruns + "<br />");
  }
  if (typeof arguments[5] === "number") {
    document.write("<strong>Runs Batted In:</strong> " + rbi + "</p>"); 
  }
}
4
  • 5
    because typeof undefinedreturns "undefined", which is not an exception. Commented Oct 24, 2013 at 17:07
  • Why would you expect it to do so? You're deliberately testing for their existence! Commented Oct 24, 2013 at 17:09
  • @Sebas Thanks, you mean being within a conditional makes it non-mandatory? Commented Oct 24, 2013 at 19:32
  • Well, by default the parameters are optional in javascript. Better said, if you don't pass any parameter to the function call, they are assumed to be undefined in the function body. It may crash if you run a piece of code that would crash on an undefined object. Testing whether typeof undefined === 'string' is not one of these pieces of code. Commented Oct 24, 2013 at 20:49

3 Answers 3

2

Any argument that is not passed appears as undefined inside the function. In JavaScript there is no method overloading.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this Jason. Pointing out method overloading helped it make sense.
1

Remember that "arrays" in JS area actually associative. So the 2, 3, ... that you put in as indexed area really just hash keys. You could also check the value of arguments[54876]. That would not fail, but return to you undefined. So even though you're thinking of your parameter array as having only three valid indices and anything else giving you something along the lines of "index out of bounds", you really have three valid entries and lookup with any other keys don't fail, but give you undefined.

Comments

1

The parameters which aren't passed a value are set to undefined, which is not an error unless you try to do something with it that cannot be done with undefined. The only thing you are doing is checking if typeof undefined === 'number', which simply returns false, but does not throw an error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.