pg 227 Accepting Any Number of Arguments

by Lucille Kenney

JavaScript

/*ACCEPTING ANY NUMBER OF ARGUMENTS*/

/* you can use arguments.length as the basis of a quick test to see that a function was called with the proper 
number of arguments: */

/*function functionName(someVar, anotherVar, yetAnotherVar) {
    if (arguments.length == 3) { alert('Good to go!');
    } else { alert('Missing something!');
    }
    console.log(length);
}

functionName('Hello, ', 'World', '!');*/

/*You can also use a for loop within the function to loop through every received argument:*/

/*for (var i = 0, count = arguments.length; i < count; i++) {
    // Do something with arguments[i].
}*/

/*The arguments variable is used all over the place in JavaScript’s built-in methods, such as the concat() 
method, which can take any number of values to be concatenated together:*/
//myArray.concat(1);
//myArray.concat(2, 3, 4);
/* There is one more way to pass a variable number of values to a function, and that’s to only pass one argument, but of type object:*/

/*  but you can also write functions to purposefully take 
a variable number of arguments, as discussed */

function showText(argObject) {
    // Use argObject.
    console.log(argObject);
}
showText({text: 'Hello, World!', bold: true, size: 12});