-1

Inside one JS file I saw:

Math.round(timeEngine.timeLeft, 10)

I've checked many documentations (some are mentioned here) and all mention that this function takes one argument, so what the second argument (10) is referring to?

https://www.w3schools.com/jsref/jsref_round.asp

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round

Maor
  • 1
  • 2
    It’s meaningless; the argument is ignored, unless `Math.round` has been redefined. You can also check the [specification](//tc39.es/ecma262/multipage/numbers-and-dates.html#sec-math.round). Where have you seen this “one JS file”? And what should we do about it? – Sebastian Simon Dec 03 '22 at 15:26
  • Because the code is wrong? – André Dec 03 '22 at 15:27
  • Depends on the context. `Math` *might* refer to something custom. Or it also *might* just be an error in the code. If that's the standard `Math.round`, you are right, it takes only one argument and the second is just ignored – derpirscher Dec 03 '22 at 15:27
  • @SebastianSimon so if JS function takes X arguments I can send X+10 and it will run perfect? so strange – Maor Dec 03 '22 at 15:27
  • 1
    @Maor Well, yes: [What happens if I call a function with more arguments than it is defined to accept?](/q/12694031/4642212). – Sebastian Simon Dec 03 '22 at 15:28
  • @Maor: if you try `window.alert(1,2,3,4,5,6,7)`, you can observe that the 2 to 7 are simply ignored. – qrsngky Dec 03 '22 at 15:30

2 Answers2

0

The Math.round() function takes one argument, which is the number that you want to round. The second argument that you saw in the code (10) is not part of the Math.round() function, and it is likely being used for a different purpose.

Dhammika
  • 61
  • 4
  • 2
    "being used for a different purpose." like what? others mentioned it will be just ignored... – Maor Dec 03 '22 at 15:28
0

Math.round() function in JavaScript takes only one parameter. But round() function is implemented differently in other languages. For example, round() function in Python takes two parameters. First parameter is number and second parameter is digits. It's the same as Number.toFixed() function in JavaScript. I think it is possible that Python developers who is not familiar with JavaScript may write like this. I hope this can help you understand.