Why is x = 7? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why is x = 7?

function myFunction(a, b) { a++; b++; return (a ,b); } var x = myFunction(5, 6); document.write(x);

18th May 2018, 6:13 AM
IZzZI
IZzZI - avatar
11 Answers
18th May 2018, 7:23 AM
Rajeeb
+ 1
return (a,b); / return statement will not return two values here it will return only b
18th May 2018, 7:12 AM
Rajeeb
0
why
18th May 2018, 7:15 AM
IZzZI
IZzZI - avatar
0
return statements sends back only one result to validate it
18th May 2018, 7:19 AM
Rajeeb
0
Thank You so much! Can u explain to me this. Why doesn't the function stop at return as return is supposed to stop the execution of a function. So shouldn't the answer be 3 rather then 5 function test(x) {while (x<5){x++;}return x;} alert(test(2));
18th May 2018, 8:05 AM
IZzZI
IZzZI - avatar
0
the last value is 5 because x is incremented to 1 and returns 5
18th May 2018, 8:13 AM
Rajeeb
0
when 2 gets incremented to 3, why doesn't the function stop and return the final value as 3?
18th May 2018, 8:47 AM
IZzZI
IZzZI - avatar
0
its while loop , it will not end until the statement becomes false
18th May 2018, 8:50 AM
Rajeeb
0
if you want to return when x=3 , put if else statement
18th May 2018, 8:51 AM
Rajeeb
0
a = 6 b = 7 returns the highest number between these 2 numbers
18th May 2018, 5:08 PM
Raul Oliva
Raul Oliva - avatar
0
Hi Raul Oliva. Just to correct your statement, function myFunction(a, b, c) { a++; b++; c++; return (a,b,c); } var x = myFunction(5, 9, 7); document.write(x); //Outputs : 8 Although highest value is 10, it only returns the last calculated value, which is 8
19th May 2018, 5:06 AM
IZzZI
IZzZI - avatar