Javascript fuction

Hi, i have a problem understanding what the second and third line of the code does.




function myFun(a,  b, ...manyMoreArgs) {
 console.log("a", a)
  console.log("b", b)
  console.log("manyMoreArgs", manyMoreArgs)
}

myFun("one", "two", "three", "four", "five", "six")

Less confusing:

function myFun(firstArgument,  secondArgument, ...restOfTheArguments) {
 console.log("a", firstArgument)
  console.log("b", secondArgument)
  console.log("manyMoreArgs", restOfTheArguments)
}

myFun("one", "two", "three", "four", "five", "six")

First line:

  • function declaration

Second line: print two things to the console:

  • string “a”
  • first argument passed to a function (string “one”)

Third line: print two things to the console:

  • string “b”
  • second argument passed to a function (string “two”)

Fourth line:

  • string “manyMoreArgs”
  • rest of the arguments passed to a function, all packed in an array (array [“three”, “four”, “five”, “six”])

Fifth line:

  • closing the body of a function
1 Like

okayyy,now i really understand.I didnt think of it that way.Thanks alot.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.