+ 1

Explain Output Of This Code?

function func(a,b=1,c=3) { return (a+b+c)%3; }; document.write(func(2,4));

7th May 2017, 7:13 AM
Mohamed Kalith
Mohamed Kalith - avatar
4 odpowiedzi
+ 6
Possible output of this code is 0. This function uses ES6 default parameter values. so when you call func(2,4) it will be called like this func(2,4,3) so, (a + b + c) % 3 = ( 2 + 4 + 3) % 3 = 9 % 3 = 0
7th May 2017, 7:22 AM
Apel Mahmod
Apel Mahmod - avatar
+ 5
% is reminder operator in Javascript. Check this link, you will know https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
7th May 2017, 6:40 PM
Apel Mahmod
Apel Mahmod - avatar
0
why 9%3 =0 ?
7th May 2017, 6:34 PM
Mohamed Kalith
Mohamed Kalith - avatar
0
% gives the remainder. when 9 is divided 3 ..we get quotient as 3 and remainder as 0.
9th May 2017, 9:38 AM
Naseef
Naseef - avatar