The return Statement in ECMAScript
Statements in ECMAScript – Part 5
ECMAScript 6
Foreword: In this part of the series I talk about the return statement in ECMAScript.
By: Chrysanthus Date Published: 13 May 2016
Introduction
Basic Function Structure
A basic function is of the form:
function functionName (param1, param2, …)
{
//statements
return [value]; // optional
}
The return statement is optional, and is usually the last statement, in many situations. In its absence, the result of the last statement (expression) is returned.
The return Statement
The return statement itself can simply be:
return;
In this case nothing is returned (undef is returned).
The return statement can be:
return value;
Here, value, can be the actual datum, e.g, a number such as, 56. It can also be a string or a structure, such as an array.
The return statement means, stop executing the function just after the return statement and return to the caller (calling function). The following script illustrates this:
<script type="text/ECMAScript">
function fn()
{
alert(1);
alert(2);
return 56;
alert(3);
alert(4);
}
ret = fn();
alert(ret);
</script>
Read and try the code. Execution of the function definition, fn(), stops at the return of 56. The statements that follow in the function definition are not executed. The calling function is, “fn();”. The called function is the definition of fn().
You can decide whether or not the statements below the return statement in a function, should be executed, using the if-construct. In the next two programs, the return statement is inside the block of an if-construct in the function.
In the following script, the statements below the if-construct are executed because the if-condition is false (read and try it).
<script type="text/ECMAScript">
var flt = 2.5;
function fn()
{
alert(1);
alert(2);
if (flt == 8.7)
{
return 56;
}
alert(3);
alert(4);
}
ret = fn();
alert(ret);
</script>
In the above function “fn()”, there is no effective return statement.
In the following script the statements below the if-block are not executed because the if-condition is true (read and try it):
<script type="text/ECMAScript">
var flt = 2.5;
function fn()
{
alert(1);
alert(2);
if (flt == 2.5)
{
return 56;
}
alert(3);
alert(4);
}
ret = fn();
alert(ret);
</script>
A function does not only have to return what has been created inside the function definition. It can also return what has been created outside the function. The following script illustrates this, with the fn() function:
<script type="text/ECMAScript">
var flt = 2.5;
function fn()
{
//some statements
return flt;
}
ret = fn();
alert(ret);
</script>
That is it for this part of the series.
Chrys
Related Links
ECMAScript BasicsECMAScript Operators
Expressions in ECMAScript
Statements in ECMAScript
Custom Objects in ECMAScript
Functions in ECMAScript
ECMAScript Date Object
The ECMAScript String Object
ECMAScript String Regular Expressions
ECMAScript Template Literal
The ECMAScript Array
ECMAScript Sets and Maps
ECMAScript Number
Scopes in ECMAScript
Mastering the ECMAScript (JavaScript) eval Function
Sending Email with ECMAScript
ECMAScript Insecurities and Prevention
Advanced Course
Advanced ECMAScript Regular Expressions
Promise in ECMAScript 2015
Generator in ECMAScript 2015
ECMAScript Module
More Related Links
Node Mailsend
EMySQL API
Node.js Web Development Course
Major in Website Design
Low Level Programming - Writing ECMAScript Module
ECMAScript Course
BACK