Return
Part of the Fundamentals section of Coddy's JavaScript journey — lesson 47 of 77.
The return statement in a function is used to specify the value or values that the function should produce as its output.
For example, the following function will output 100:
function functionName() {
return 100;
}To pass the value to a variable, write:
let number = functionName();Now the number variable will hold 100 because this is what the function returned.
Challenge
EasyCreate a function called square that takes a single parameter n and returns its square. Then, call the function with the input value (given) and store the result in a variable called result. Finally, print the value of result.
Cheat sheet
The return statement specifies the value a function produces as output:
function functionName() {
return 100;
}To capture the returned value in a variable:
let number = functionName();Try it yourself
let num = parseInt(inp); // Don't change this line
// Type your code below
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Type Coercion7Bill Split Calculator
Welcome MessageCalculating The Tip And Total2Variables
NumbersStringBooleanNaming ConventionsEmpty VariablesRecap - Initialize VariablesConstants3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsComparison OperatorsStrict vs Loose EqualityRecap - Simple Math6Basic IO
OutputOutput with VariablesType Conversion - Part 1Type Conversion - Part 2Recap - Till 120Recap - True or False