How can we use return function twice or more at a time

Why we can't use return twice and more in program for example: var a = parseInt(prompt("enter your 1st number")); var b = parseInt(prompt("enter your 2nd number")); function add (n1,n2){ var total= n1+n2; var sub= n1-n2; return total ; //here we can't use together return sub ; /* how we can use this if i want to execute both iin same Function? */ } document.write(add(a,b) ); https://code.sololearn.com/WQYi37noWgeQ/?ref=app https://code.sololearn.com/WQYi37noWgeQ/?ref=app

1/23/2020 11:02:20 AM

piyush raj

2 Answers

New Answer


piyush raj An function only return one value but you can use if and else statements to do so like this is an example add it with your code. var entry = prompt("Choose addition or subtraction"); if(entry=="addition"){ var a =prompt("Enter number 1") var b =prompt("Enter number 2") var c =(+a)+(+b); document.write("The answer is "+c); }else if(entry=="subtraction"){ var d =prompt("Enter number 1") var e =prompt("Enter number 2") var f =d-e document.write("The answer is "+f) }else{ alert("Error!"); }


You can't, it is only possible once.