This JavaScript example demonstrates how to write and call a function with default arguments.
<!DOCTYPE html> <html> <head> <title>XoaX.net's Javascript</title> </head> <body> <script type="text/javascript" src="FunctionsDefaultArguments.js"></script> </body> </html>
// A function with three default arguments function F(X = 11,Y = 7,Z = 3) { document.writeln("X = " + X + " : Y = " + Y + " : Z = " + Z + "<br />"); return X*Y*Z; } document.writeln("F(2, 5, 9) = " + F(2, 5, 9) + "<br /><br />"); document.writeln("F(2, 5) = " + F(2, 5) + "<br /><br />"); document.writeln("F(2) = " + F(2) + "<br /><br />"); document.writeln("F() = " + F() + "<br /><br />"); document.writeln("<br /><br />"); // A function with one default argument function G(X, Y = 1) { document.writeln("X = " + X + " : Y = " + Y + "<br />"); return X + Y; } document.writeln("G(5, 7) = " + G(5, 7) + "<br /><br />"); document.writeln("G(5) = " + G(5) + "<br /><br />"); // The first argument is undefined because it has no default. document.writeln("G() = " + G() + "<br /><br />");
© 20072024 XoaX.net LLC. All rights reserved.