JavaScript Quick Tip of the Day

I want to lay a tip on you that won’t thrill you much if you’re a JS pro, but if you’re

still fairly new to the language, as many of us are, you will find it interesting and

possibly quite handy. The topic at hand is an attribute of core JavaScript function objects

called arity. And no, I didn’t leave any letters out. The name of the attribute (or

property) is arity, plain and simple, and it means argument-count.

What can you do with it? Well, have you ever noticed the peculiar ease with which

languages like JavaScript deal with functions that have arbitrary numbers of passed-in

arguments? I mean, when I was learning C, this was a Big Deal. You had to know in advance

how many arguments a function was going to have; otherwise it meant taking special steps,

blah-blah, blah. And now along comes this little old scripting language called JavaScript,

with full OOP power rolled under its armpit, and it handles functions with any number of

arguments, left undetermined until runtime. Heck, call any function with any number

of parameters (of any type, even), at runtime, and most JS interpreters will not

complain. It’s downright bizarre!

Wouldn’t you like to know how to be able to write functions that do that? Sure you would.

And it’s pathetically easy. Here’s how to do it.

Say you want to write a function, we’ll call it SumOfAllArgs(), that basically

just numerically adds together all of the arguments you pass in to it, and returns the sum.

Declare it (oops, there I go talking like a C programmer again…) as follows:

function SumOfAllArgs(){argCount = SumOfAllArgs.arity;var sum = 0;
for (i = 0 ; i < argCount; i++ ) sum += SumOfAllArgs.arguments[i];
app.alert('arity = ' + SumOfAllArgs.arity +', and return value is ' + sum);
return sum;}

Notice I didn’t declare it as taking any arguments. Just ‘void’ as far as args are

concerned. Right? Well, when you go to use this function, guess what? You can flog it like

any other JavaScript workhorse function. Feed it any number of arguments you want. Try it!

Put the foregoing code inside a script (in a PDF form, for example) and call the
function with a handful of numeric arguments. See what pops up on the screen.

Input parameters and return values

Let’s say you pass four numbers as arguments: 10, 11, 11, and 12. But I want to emphasize, you can pass any quantity of arguments to the function, at

runtime, and the function will never complain. Of course, you should try not to pass it

undefined values. But JavaScript is more robust than you think. If you pass no args at all

(not even zero), you’ll get reasonable values for arity and return. (Namely, zero and zero.)

No complaints. If you pass zero as the sole parameter, you’ll get arity of one and a return

value of zero. Try to guess what happens if you pass an argument of ‘true.’ For extra

credit, predict the code’s behavior if you pass in an argument value of ‘test()’ (without

quotes). Hint: No, it doesn’t recurse endlessly.

You May Also Like

About the Author: Kas Thomas

Leave a Reply