DONATE  
MENU
HTML
AJAX
JSON
JAVASCRIPT

JavaScript Tutorials

INTRODUCTION

» Introduction to JavaScript

» Practice IT Editor

» JavaScript Example

» JavaScript Placement

» JavaScript Syntax

» JavaScript Statements

» JavaScript Input

» JavaScript Output

» JavaScript Comments

» JavaScript Variables

» Variables' Scope

» JavaScript Operators

» JS Expressions & Concatenation

» JavaScript Data Type

» JavaScript Numbers

» Number Object

» JavaScript Strings

» String Object

» JavaScript Booleans

» Boolean Object

» JavaScript Functions

» JavaScript Arrays

» Array Object - Properties

» Array Object - Methods

» JavaScript Objects

» JavaScript Undefined

» JavaScript Nulls

CONDITIONAL TESTING

» If Statement

» If Else Statement

» JavaScript Ternary Operator

» Switch Statement

LOOPS

» For Loops

» While Loops

» Break & Continue

» For...In Loops

» do...while Loop

FUNCTIONS

» JavaScript Functions

» Creating Functions

» Functions' Parameters

» Functions' Arguments

» Function Invocation

» The Return Keyword

» The Arguments Object

» Callback Functions

» Arrow Functions

ARRAYS

» JavaScript Arrays

» Creating Arrays

» Accessing Arrays

» Looping Arrays

» Multi - dimensional Arrays

OBJECTS

» JavaScript Objects

» Creating Objects

» Prototype Definition

» Accessing Objects

» The this Keyword

AJAX

» Introduction to AJAX

JSON

» Introduction to JSON

JAVASCRIPT DATES

» The Date Object

» Date Methods

» Timing (SetInterval & SetTimeout)

MATH OBJECT

» Math Object

» Math Methods

HTML / JS DOM

» DOM Introduction

» Accessing HTML Elements in DOM

» Get And Change Elements' Contents & Values

» Change Styling - DOM CSS

» DOM Attributes

» JavaScript Event Listeners

» DOM Event Listeners

» JS Events

MISCELLANEOUS

» JS Strict Mode

» JavaScript Hoisting

» JavaScript let keyword

» Reserved Keywords

» JS Best Practices

» JavaScript vs ECMAScript

JAVASCRIPT EXAMPLES

» JavaScript Calculator

.. » JavaScript » Functions » The Arguments Object
Share Tweet WhatsApp

The Arguments Object

The arguments object or default arguments variable is an array that contains all the values/arguments passed to a function during invocation. Remember an array is a value that can hold more than one value of any data type. So, whenever we invoke and pass arguments to functions and even functions without parameters, an argument object exists that can be used in accessing such arguments.
<script>
function args (){ //No parameters but can have an arguments object when you pass arguments when invoking it
alert(arguments);
}

args("Hello World", 5, 6);
</script>

Copy PRACTICE IT

<script>
function args (num){//One parameter but we can have more than one argument and access them via the argument object
alert(arguments);
}

args(1, 3, 3);

</script>

Copy PRACTICE IT

Every function has an argument object by default that can be used in accessing all it arguments in the function definition. The arguments is an array variable that can be use in the function definition just as you use parameters.
<script>
function myFunc(){
var argslen = arguments.length;
if (argslen > 0){
for (var i = 0; i<argslen; i++){
alert("Argument "+(i+1)+" is "+arguments[i]);
}
}
else
{
alert("No arguments was passed.");
}
}

myFunc("stack","beginner","developer");
</script>

Copy PRACTICE IT

In above, we created a function without any parameters but as a result of the arguments(an array) object, we were able to access whatever argument if any passed when invoking the function. To understand the usefulness of the argument object, let analyze an assumption. Assuming a function was created to find the highest value among whatever number of arguments was passed to it during invocation, you won't be able to define a definite number of parameters. We can therefore use the arguments object to achieve our aim as shown below:
<script>
function findMaxMin(){
var len = arguments.length; //number of arguments passed
if (len > 0){ //check if arguments are more than 0 i. e if any arguments were passed
var max = arguments[0]; //assign first argument to max
var min = arguments[0]; //assign first argument to min

//THE LOOP
for (var i=0; i<len; i++){
if (arguments[i] > max){ //if argument is greater than the max
max = arguments[i]; //it is maximum number
}
else if (arguments[i] < min){ //if argument is less than min
min = arguments[i]; //it is the minimum number
}
else
{
}
}
alert("The minimum number is "+min+" while the maximum number is "+max); //output
}
else
{
alert("No arguments were passed."); //no argument passed
}
}

//invoke function with whatever parameters
findMaxMin(2, -4, 67, 8, 1, 30, 5, 76);
</script>

Copy PRACTICE IT

Above, you can see the importance of the arguments object. This means we may decide or not to specify parameters for a function but still be able to access and use any argument passed when invoking such functions.
NOTE:
  • If you plan on using the arguments object at any particular time in a function, then be sure to ensure you assign no value to any variable with name arguments before such use else the default arguments object values will be erased in favour of the new value.
SUMMARY
  1. The JavaScript arguments object is an array containing all arguments assigned when invoking a function.
  2. With the object, you can therefore have few or no parameters in a function but still access whatever arguments passed whenever the function is invoked.
  3. Every function have an arguments object. It is either empty when no arguments were passed or not empty.
Take a short quiz on The Arguments Object.
Ask a question or tell us if The Arguments Object is easy or not.
cent?

Join our 1,090 subscribers to receive tutorials' updates!

  We won't spam your mail box. We hate it.





StackBeginner 2021 © Joseph Omamode