search

Home  >  Q&A  >  body text

javascript - What does arguments.length mean in for loop?

Don’t quite understand the meaning of this code? Ask God to explain

function sum(){
    var sumg=0;
    for(var i=0;i<arguments.length;i++){
        sumg+=arguments[i];
    }
    return sumg;
}
alert(sum(1,2,3,4,5));
ringa_leeringa_lee2840 days ago526

reply all(2)I'll reply

  • 世界只因有你

    世界只因有你2017-05-19 10:25:23

    arguments 就是参数的意思
    这个函数是为了求和,所以参数的数量不是固定的,可能是 sum(1,2),可能是 sum(1,2,3)
    Since the parameters are not fixed
    then we need to get the parameters
    Look at the following code

    for(var i=0;i<arguments.length;i++){
        sumg+=arguments[i];
    }

    For example, we enter sum(1,2)sum(1,2)
    这时 arguments.length=2
    arguments[i] 的值分别是12At this time, the values ​​of arguments.length=2

    arguments[i] are 1 and 2

    This achieves the purpose of passing parameters🎜

    reply
    0
  • PHPz

    PHPz2017-05-19 10:25:23

    arguments is a built-in attribute of the current function. It is an array-like array that stores the actual parameters of the incoming function. The length is the number of incoming actual parameters. The meaning of this code is to find 1+2+3+4+ Value of 5

    reply
    0
  • Cancelreply