JavaScript Function with Unlimited Arguments

Declared Arguments

Usually, we provide an argument list when declaring a JavaScript function. Therefore, creating a function that checks for the existence of an argument is pretty simple. For example:

function popup (url, framename, attr) {
    if (!url) url = '';
    if (!framename) framename = 'MyPopup';   
    if (!attr) attr = "width=800px,height=600px";
   
    window.open(url, framename, attr);   
    return false;
};

Unlimited Arguments

However, in some cases you might want to accept an unknown number of arguments. For this case, JavaScript provides an array-like object corresponding to the arguments passed to the function called arguments. For Example:

function unlimited (){
    for( var i = 0; i < arguments.length; i++ ) {
        alert("Argument [" + i + "]: " + arguments[i]);
    }    
}

blog comments powered by Disqus