Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

JavaScript arguments object

Suggested Videos
Part 26 - Local and global variables in javascript
Part 27 - Closures in JavaScript
Part 28 - JavaScript closure example



The JavaScript arguments object is a local variable available within all functions. It contains all the function parameters that are passed to the function and can be indexed like an array. The length property of the arguments object returns the number of arguments passed to the function.



JavaScript arguments object example :
function printArguments()
{
    document.write("Number of arguments = " + arguments.length + "<br/>")
    for (var i = 0; i < arguments.length; i++)
    {
        document.write("Argument " + i + " = " + arguments[i] + "<br/>");
    }
    document.write("<br/>");
}

printArguments();
printArguments("A", "B");
printArguments(10, 20, 30);

Output : 
javascript arguments example

Is it possible to pass variable number of arguments to a JavaScript function
Yes, you can pass as many arguments as you want to any javascript function. All the parameters will be stored in the arguments object.

function addNumbers()
{
    var sum = 0;
    document.write("Count of numbers = " + arguments.length + "<br/>")
    for (var i = 0; i < arguments.length; i++)
    {
        sum = sum + arguments[i];
    }
    document.write("Sum of numbers = " + sum);
    document.write("<br/><br/>");
}

addNumbers();
addNumbers(10, 20, 30);

Output : 
variable number of parameters javascript

The arguments object is available only inside a function body. Attempting to access the arguments object outside a function results in 'arguments' is undefined error. Though you can index the arguments object like an array, it is not an array. It does not have any Array properties except length. For example it does not have the sort() method, that the array object has. However, you can convert the arguments object to an array.

Converting JavaScript arguments object to an array
function numbers() 
{
    var argsArray = Array.prototype.slice.call(arguments);
    argsArray.sort();
    document.write(argsArray);
}

numbers(50, 20, 40);

Output : 20, 40, 50

Converting JavaScript arguments object to an array using array literals
function numbers() 
{
    var argsArray = [].slice.call(arguments);
    argsArray.sort();
    document.write(argsArray);
}

numbers(50, 20, 40);

Output : 20, 40, 50

JavaScript tutorial

3 comments:

  1. why its not sorting

    function Printargs()
    {
    var argsArray = Array.prototype.slice.call(arguments);
    argsArray.sort();
    document.write(argsArray);
    }

    Printargs(30, 2, 5, 3, 7, 4, 8, 1);

    ReplyDelete
    Replies
    1. By default, the Array Sort method in Javascript does String Sort, so the values which are of type numeric are sorted as per string order. So output is 1,2,3,30,4,5,7,8.

      To get desired output, you need to pass in a function which computes how to sort the numbers.

      Delete
  2. Ascending order
    --------------------------------
    function Printargs() {
    var argsArray = Array.prototype.slice.call(arguments);
    argsArray.sort(function (a, b) {return a-b });
    document.write(argsArray);
    }

    Printargs(30, 2, 5, 3, 7, 4, 8, 1);

    Descending order
    ------------------------------------
    function Printargs() {
    var argsArray = Array.prototype.slice.call(arguments);
    argsArray.sort(function (a, b) {return b-a });
    document.write(argsArray);
    }

    Printargs(30, 2, 5, 3, 7, 4, 8, 1);

    ReplyDelete

It would be great if you can help share these free resources