Javascript – iterate over function arguments

Sometimes it is useful to iterate over function arguments programmatically. Function arguments can be access using arguments variable. Here is code snippet for this.

<script type="text/javascript">
function f1() {
  document.write("------f1 called typeof(arguments)=" +typeof(arguments) + "<br/>");
  for (var i=0; i < arguments.length; i++) {
    document.write("arg" + i + "=" + arguments[i] + "<br/>");
  }
}
f1("arg1");
f1("arg1", "arg2");
</script>
refresh

Note that Javascript method forEach cannot be use on arguments.

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments