Sometimes a function only displays a result, but often you want a function to send a value back so it can be used later in the program. This is done using the return statement.
When a function returns a value, it becomes more useful because the returned result can be stored in a variable, displayed on the screen, or passed to another function.
Learning how to return values is an important step toward writing real and practical JavaScript programs.
Why Returning Values is Important
- It allows functions to send results back
- It makes functions reusable in many places
- It helps connect different parts of a program
- It supports calculations and data processing
- It makes code more flexible and powerful
Without return values, functions would be limited in real applications.
This function only prints the result. You cannot store the result or use it later in the program.
This limits what you can do with the output.
Now the function returns the sum. The returned value is stored in the variable result and then displayed.
This makes the function more useful and reusable.
This example shows how a function returns a value that is later used in another part of the program.
By mastering return values, students gain the ability to write flexible, reusable, and professional JavaScript functions that power real-world applications.