JavaScript Functions: The return Statement

The return statement in the javascript function is the very explanation of how the function value should be called and returned. Once this return is invoked the function will stop all the remaining work of the code block and execute the return statement respective to its call of the function operation with the value provided by the invoker of the function for the return statement.

The return statement should be called by the last step of the code block of the function because whenever it is called the code stops to the point of the return statement and will not continue the code beyond the return statement which will be unreachable at this point.

We will have a clear view of what a return statement is via example.

Syntax

return expression;

Example

function cubeRoot(a)
{
    return a * a * a;
}
console.log(cubeRoot(4))

Output: 64

So we have used the return statement in this code to return the cube value of a given number. Thus invoked by giving the value of 4 by the invoker and the function stops whatever it is doing and performs the return statement operation without any question.

NOTE: Always use the return statement at the end of the function code block

Fri, 10/22/2021 - 13:00

Authored by

Ashwin is working with iVagus as a Technical Lead. He has experience in various technologies like Cypress, JavaScript, TypeScript, Rest Assured, Selenium, Cucumber, and Java.
Tags