length Property (Function)
Collapse the table of content
Expand the table of content

length Property (Function) (JavaScript)

 

Gets the number of arguments defined for a function.


functionName.length

The required functionName is the name of the function.

The length property of a function is initialized by the scripting engine to the number of arguments in the function's definition when an instance of the function is created.

What happens when a function is called with a number of arguments different from the value of its length property depends on the function.

Example

The following example illustrates the use of the length property:

function ArgTest(a, b){
    var s = "";

    s += "Expected Arguments: " + ArgTest.length;
    s += "<br />";
    s += "Passed Arguments: " + arguments.length;

    return s;
}

document.write(ArgTest(1, 2));

// Output: 
// Expected Arguments: 2
// Passed Arguments: 2

Requirements

Supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards, Internet Explorer 11 standards. Also supported Store apps (Windows 8 and Windows Phone 8.1). See Version Information.

Show:
© 2016 Microsoft