Print Shortlink

arguments in JavaScript

Ext JS 4 involves calling this.callParent(arguments) from the initComponent method of a view component. For quite some time I was thinking that arguments is a pre-defined variable in Ext JS 4 before I started reading  the book  JavaScript: The Good Parts”.   Very interesting book for those who want to start taking JavaScript as a serious programming language. 
arguments is  a built-in parameter available in a function in JavaScript. It’s an array that is used to access the parameter list of the function, which basically says you don’t really have to formally declare arguments for a function, but still access the values passed, using the ‘arguments’.
Say you have a method to concatenate all the values passed to a method in JavaScript. You can write it like below: 

function concat(){
   var result = “”; 
   for(var i=0;i<arguments.length;i++){
        result += arguments[i] + ” “;
   return result;
document.write(concat(“This”,”is”,”cool”)); //Output is This is cool

You can see that arguments is a built-in array parameter that can be used to access the parameters passed when the function is invoked. In case you want to avoid concatenating anything other than a string, reflection can be tossed in.

  if(typeof arguments[i] == ‘string’)
       result += arguments[i] + ” “;
But what is strange is that the ‘arguments’ is really not an Array instance in the true sense. It doesn’t have methods like slice, push, pop associated with the Array class. So need to be a bit careful while using ‘arguments’.

Leave a Reply