简体   繁体   中英

Passing arguments in a javascript function is not working

I am bit unclear in understanding why this below piece of code is not showing the message box,

http://jsfiddle.net/KendoDev/p8Mk2/

 function findMax(var x, var y) {

//alert("x is:" + x + " y is: " +y);
    var max = 0;
    if(x>y)
         max = x;
    else
         max = y;
    alert("max is: " + max);
    return max;
} 
var c=4,d=9;
var m = findMax(c,d);

您的函数语法错误:在参数列表中不需要var

function findMax(x, y) {

Your javascript isn't syntactically correct - function findMax(var x, var y) should just be function findMax(x, y) .

In general, if you write some javascript and nothing happens when you run it, it probably means something went wrong syntactically, and you should check your browser's Error Console. (In this case, it tells you that the function definition is where the error occurred, right on the first "var".)

Remove var from arguments. Like this:

function findMax(x, y) {

//alert("x is:" + x + " y is: " +y);
    var max = 0;
    if(x>y)
         max = x;
    else
         max = y;
    alert("max is: " + max);
    return max;
} 
var c=4,d=9;
var m = findMax(c,d);

you do not need to put a var

see updated fiddle

http://jsfiddle.net/p8Mk2/2/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM