Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

4. Function() > The Function Instance length Property and arguments.length

The Function Instance length Property and arguments.length

The arguments object has a unique length property. While you might think this length property will give you the number of defined arguments, it actually gives the number of parameters sent to the function during invocation.

Live Code

<!DOCTYPE html><html lang="en"><body><script>

var myFunction = function(z, s, d) {
   return arguments.length;
};

console.log(myFunction()); /* logs 0 because no parameters were passed 
                           to the function */

</script></body></html>

Using the length property of all Function() instances, we can actually grab the total number of parameters the function is expecting.

Live Code

<!DOCTYPE html><html lang="en"><body><script>

var myFunction = function(z, s, d, e, r, m, q) {
   return myFunction.length;
};

console.log(myFunction()); // logs 7

</script></body></html>

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint