Handling an unknown number of arguments I'm writing a function that takes between one and three arguments (function callback [, number limit] [, regexp pattern]). Since the second two are both optional, I need to determine which argument is being passed when the function is supplied with only two. Below is my solution, but it seems overly verbose. Is there a cleaner way to do this?

  var get_queued_pages = function() {
    if(arguments&gt;0){
        var callback = arguments[0]; 
        if(arguments.length == 3){ //all arguments are being passed     
        var limit    = arguments[1];
        var pattern  = arguments[2];
        }
        else if(arguments.length == 2){ //only 2 arguments passed
        if(typeof(arguments[1]) == "number"){ //check if the second one is limit or pattern             
            var limit = arguments[1];//no pattern defined; second argument is limit
        }
        }
        else{
        var limit = 1;
        }
    }
    //do stuff

}
function(cb,limit,pattern) {
    limit = typeof(limit) === "undefined" ? 1 : limit;
    //do stuff
}


Calling a function with less arguments than declared will just fill them with undefined. You can then check if that is the value, if so fill in your default ones.
The I have above will have the same affect as yours, there are always three variables (even in your code, Javascript variables are function scope, all of the var declarations are pulled to the top.).
The variables:
cb - unchanged
limit - set to 1 if not supplied
pattern - unchanged (undefined if not supplied)

EDIT:
I just saw your comment on reordering
you can do the following

  function foo(cb,limit,pattern) {
        if(limit instanceof RegExp) {
            pattern = limit;
            limit = 1;
        }
        else {
            limit = typeof(limit) === "undefined" ? 1 : limit;
        }
        //do stuff
    }