Video Lesson

Lesson Notes

Returning Values from Functions in JavaScript

Using return lets functions output a value you can use elsewhere instead of just logging it.

Defining a Function with return

1function add(numberOne, numberTwo) { 2 return (numberOne + numberTwo); 3}
  • return sends the result back to the caller.
  • Parentheses around the expression are optional but group the operation.

Capturing the Returned Value

Assign the function call to a variable:

1const result = add(3, 45); 2console.log(result); // 48 3 4const resultTwo = add(12, 7); 5console.log(resultTwo); // 19
  • The value returned by add() is stored in result or resultTwo.
  • You can then log, manipulate, or pass these values elsewhere.

Why Use return?

  • Flexibility: Use the output in calculations, conditions, or further function calls.
  • Reusability: Call the same function with different inputs and handle the outputs as needed.
1function multiply(a, b) { 2 const product = a * b; 3 return product; 4} 5 6const area = multiply(5, 10); 7console.log(`Area: ${area}`); // Area: 50

Summary

  • Use return inside a function to send back a value.
  • Capture returned values in variables for further use.
  • Makes functions more powerful and composable.

AI Assistant

Sign In Required

Sign in and subscribe to use the AI assistant for instant help with your lessons.

Sign In

Return values from functions

JavaScript functions that use the `return` keyword to output values that can be utilized throughout your code. In this lesson, you'll learn how to define functions that return results, capture those values for further manipulation, and enhance the flexibility and reusability of your code.

3m 0s

Course Content

0 of 32 lessons completed
6. Promises, Async and Await
0 of 0 completed
7. The DOM
0 of 0 completed