Javascript Function Argument Passing Question 2

Introduction

What is the output of the following code?

function threeArguments(a, b, c) {
    return "We expect 3 arguments: " + a + ", " + b + ", " + c;
}
function threeArguments(a,b,c,d) {
    return "Now we expect 4 arguments: " + a + ", " + b + ", " + c + ", " + d;
}
console.log( threeArguments(1, 2, 3) );


Now we expect 4 arguments: 1, 2, 3, undefined

Note

// Demonstrating the effect of attempted function overloading
function threeArguments(a, b, c) {
    return "We expect 3 arguments: " + a + ", " + b + ", " + c;
}

// Now we try to overload the function
function threeArguments(a,b,c,d) {
    return "Now we expect 4 arguments: " + a + ", " + b + ", " + c + ", " + d;
}

// Now we attempt to use the first version
console.log( threeArguments(1, 2, 3) ); // "We expect 4 arguments: 1, 2, 3, undefined"

We see that the reference to threeArguments has been overridden.




PreviousNext

Related