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.
- <!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>
- <!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