Javascript: Function length vs arguments

One of the reasons quite a number of programmers don’t like Javascript because it’s weird, behaving differently on different browsers.. and hard to debug.. However, it’s quite fun or even addictive to learn JS also for that reason. Below is a brief on two mysterious properties of Javascript functions : length, and arguments.

arguments :
- available : only inside a function’s body
- is : an array of parameters/arguments provided when the function is called.

length :
- available : both in/outside a function’s body. Could be called using : functionName.length
- is : the total number of arguments declared in the function’s signature

Below is the demo snippet showing how to use these properties:

var myFunction = function(a, b, c, d, e) {
		// total number of params declared : TRUE
		alert(myFunction.length == 5);
		// real number of arguments provided : TRUE
		alert(arguments.length == 2);
		// you can also refer to the function using its name : TRUE
		alert(myFunction.arguments.length == 2);		

// how to refer to the arguments provided ?
		alert(a === 'x' && arguments[0] === 'x');
		alert(b === 'y' && arguments[1] === 'y');
// myFunction is called with two arguments only
myFunction( 'x', 'y' );
// total number of params declared : TRUE
alert(myFunction.length == 5);

// undefined : FALSE
alert(myFunction.arguments == 2);

Have fun,


One thought on “Javascript: Function length vs arguments”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>