Arguments

The code inside a function often needs information passed to it.
You can define this information by adding parameters to the declaration allowing you to pass in arguments.
When a variable is passed to a subroutine or function it is called an argument.
The value used in place of the parameter when we make the procedure call is called an argument.


too many - ignored
too few - undefined


function add() { 
   var tempValue = 0;
   for (I = 0; I < arguments.length; i++)
   {
      tempValue += arguments[i];
   }
   return tempValue;
}
var myTotal = add(1,2,3,4,5,6);


Passing in Arguments

Method 1 - using individual variables

function myFunction(arg1, arg2, arg3) { 
   return arg1 + arg2 + arg3;
}
const myString = myFunction("first", "second", "third");

Method 2 - passing in an object and then use object destructuring to pull out the individual arguments

function myFunction(arguments) { 
   return arguments.arg1 + arguments.arg2 + arguments.arg3;
}
const myString = myFunction(
   { arg1: "first",
     arg2: "second",
     arg3: "third"
   }




© 2023 Better Solutions Limited. All Rights Reserved. © 2023 Better Solutions Limited TopPrevNext