return
return is a command that returns a value.
Executing statements inside a function and returning exits the function. And return what's in the return statement
For example,
function hey(a,b){ add = a+b; return add; }
If you write like above
The hey function returns the value of the add variable.
Of course, to see the returned value, you must call the function in the output text.
document.write(hey(1,2));
If you print as above, add is 3 in the return statement.
Source
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>JavaScript</title> <script> function hey(a,b){ add = a+b; return add; } document.write(hey(1,2)); </script> </head> <body> </body> </html>
So in the source above, let's put 20 instead of add in the return statement. Doing so will return 20.
Source
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>JavaScript</title> <script> function hey(a,b){ add = a+b; return 20; } document.write(hey(1,2)); </script> </head> <body> </body> </html>
end