I wanted to know how it works exactly where it needs to be used within some scope like if etc.
I wanted to know how it works exactly where it needs to be used within some scope like if etc.
As this documentation says, return
:
undefined
will be returned. function retornaValor() {
return 1;
}
console.log(retornaValor());
The above code prints 1
on the console.
function retornoVazio() {
return;
}
console.log(retornoVazio());
The above code prints undefined
on the console.
On the other hand, if you try to use return
outside of a function, in a code executed in a browser, you will receive an error. For example, in Chrome, I got:
Uncaught SyntaxError: Illegal return statement
{}
Even if used within a block of code, return
will terminate the current function or cause the error as already described. There is no concept of return of a block. Example
function f(val) {
if (val) {
return 1;
} else {
return 2
}
}
The above code will return 1 or 2 for the function, regardless of the keys.
return
Some people believe that each method / function should have a single exit point. Consider the following example:
function f(a, cond) {
if (cond) {
return -2;
} else {
for (var i = 0; i < a.length; i++) {
if (a[i] == 1) return i;
}
}
return -1;
}
This function is small, but soon there may be dozens of conditions and returns
, making the code difficult to understand.
An alternative would be as follows:
function f(a, cond) {
val retorno = cond ? -2 : -1;
if (!cond) {
for (var i = 0; i < a.length; i++) {
if (a[i] == 1) {
retorno = i;
break;
}
}
}
return retorno;
}
Obviously it's a matter of personal opinion. In particular, I believe the problem is to create a clear logic and not the amount of returns
.
You can use return
for 2 types of situations.
1. Return a value.
2. Stop the execution flow of the current function.
1. The usage situation is when you need to validate, calculate or modulate the code.
function Calculadora(valor1, valor2, operacao) {
var resultado = 0;
if (operacao == "+")
resultado = Soma(valor1, valor2);
// ...
return resultado;
}
function Soma(valor1, valor2) {
return parseInt(valor1) + parseInt(valor2);
}
2. The usage situation is when you no longer need to continue running the current function, the result is already determined.
function ExibeValor(valor) // código apenas didático, sim é feio.
{
if (parseInt(valor) <= 10) {
alert('O valor é menor/igual a 10.');
return;
}
if (parseInt(valor) > 10 && parseInt(valor) <= 20) {
alert('O valor é maior que 10 e menor/igual a 20.');
return;
}
if (parseInt(valor) > 20 && parseInt(valor) <= 30) {
alert('O valor é maior que 20 e menor/igual a 30.');
return;
}
if (parseInt(valor) > 30 && parseInt(valor) <= 40) {
alert('O valor é maior que 30 e menor/igual a 40.');
return;
}
}
Suppose you call the function by passing the value 5
, you will enter the 1st if
and you do not have to execute all other if
to know the function return . This improves the execution time and makes processing less expensive.
The return in javascript is similar to the return in Java and C #, which are the languages I know of, as long as Delphi returns the little I know works differently , and when going through a return in javascript the function (or the current scope) is aborted not proceeding in the stream, and returning the value to its invocator, this behavior can also be perceived using a debug in the browser like FireBug , or the native Google Chrome
function TesteRetorno(param){
if(param > 50){
return "Maior que 50";
}
if(param > 10){
return "Maior que 10";
}
return "Menor que 10";
}
alert(TesteRetorno(2));