Function Arguments

by Desiree Guercio

HTML

<p id="demo"></p>

<script>
function myFunction() {
    document.getElementById("demo").innerHTML = "Hello World!";
}
myFunction ();
</script>

CSS

function somma() {
    var numero,
    numeroDato = arguments.length,
        total = 100;

    for (numero = 0; numero < numeroDato; numero += 1) {
        total += arguments[numero];
    }

    return total;
};

console.log(somma(45,65));

JavaScript

// Use arguments property to allow for a dynamic
// number of arguments
function sum() {
    var i,
    n = arguments.length,
        total = 0;

    for (i = 0; i < n; i += 1) {
        total += arguments[i];
    }

    return total;
};

console.log(sum(1, 2, 3, 4, 5));

// Make arguments an array to utilize array methods
// like join() or pop()
function concatenator() {

    var args = Array.prototype.slice.apply(arguments);

    return args.join("-");

}

console.log(concatenator(1, 2, 3, 4, 5));

// Editing arguments directly will affect your argument variables
// What will happen here?
function breakMyArgs(arg1) {

    arguments[0] = 2;

    console.log(arg1);

}

breakMyArgs(1);