18225

Optional arguments in JavaScript

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>"); } }

Answer1:

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

Answer2:

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.

Answer3:

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.

Recommend

  • UICollectionview scroll horizontal and vertical
  • Is there a greater chance to collide when comparing GUIDs based on a hash vs \"Guid.NewGuid()?
  • wordpress query - next two events by metadata date
  • Find symbol dependencies in source code [closed]
  • Zend Framework 2, Module Redirect
  • Why does PHP appear to evaluate this condition incorrectly?
  • TensorFlow C++, runtime issue
  • Contact form problem - I do receive messages, but no contents (blank page)
  • Use allowDiskUse in criteria query with Grails and the MongoDB plugin?
  • How to write string.Contains(someText) in expression Tree
  • What is Closure Compiler?
  • Many to many relationship core data query for specific collection
  • Ruby: FileUtils.cp truncates file; FileUtils.mv it does not?
  • DependencyObject.AssociatedObject is always null
  • Update varbinary(MAX) field in SQLServer 2012 Lost Last 4 bits
  • when does setTimeout start executing in a inline
  • How do I register classes by both interface and namespace with Windsor?
  • JavaScript IE rotation transform maths
  • What causes the runtime difference in this trivial fortran code?
  • Is there a package like bigmemory in R that can deal with large list objects?
  • netsh acl setting (need alternative method - registry settings?)
  • Who propagate bugfixes across branches (corporate development)?
  • ViewController With Transparent Background Entering Current ViewController With Push Transition
  • How do I get HTML corresponding to current DOM tree?
  • JQuery Internet Explorer and ajaxstop
  • JSON response opens as a file, but I can't access it with JavaScript
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • How to add date and time under each post in guestbook in google app engine
  • Perl system calls when running as another user using sudo
  • Change an a tag attribute in JavaScript based on screen width
  • jQuery tmpl and DataLink beta
  • coudnt use logback because of log4j
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • To Get the radio button value in ruby on rails
  • java string with new operator and a literal