Don't understand the return statement in JS?

Is it possible to cheloveskom language to explain what makes a return, here's an example code.

getRectArea function(width, height) {
if (width > 0 && height > 0) {
return width * height;
}
return 0;
}
April 4th 20 at 00:36
4 answers
April 4th 20 at 00:38
Solution
See how to call/use this function.

var a = getRectArea( 3, 4 );
Here the variable a will be the value that a function returned something that after return it was. In your example this is 3 * 4, ie the number 12. Cheers, the variable a is now number 12

And as soon as executed return – all, then the function is not performed, it was a way out.
function test() {
return;
 alert('Hello world!'); // this line never executes
}
And what is it?
After all, you can not just write then down :)
- moshe.Cri commented on April 4th 20 at 00:41
@moshe.Cri, for example when we have some parameters to a function, but they are wrong, in this case, it is more logical to return an error and abort the function execution.

Or, for example, at delegation events:
document.addEventListener('click', e => {
 const myButton = e.target.closest('.my-button');
 // It makes no sense to perform a function, the event was not on the required element.
 if (!myButton) return;

 // ....
}):
- Sallie_Kub commented on April 4th 20 at 00:44
April 4th 20 at 00:40
Returns the value and terminates the function.

But concrete given you code - rubbish, because 0 multiplied by anything is still 0, ie the test is absolutely unnecessary.
But concrete given you code - rubbish, because 0 multiplied by anything is still 0, ie the test is absolutely unnecessary.
Not superfluous, since multiplication with negative numbers does not give a 0, but this function will return 0 for any negative argument - Rhiannon_Goldner commented on April 4th 20 at 00:43
x = getRectArea(width, height);
you have to understand this:
if (width > 0 && height > 0) {
 x = width * height;
} else {
 x = 0;
}
By the way, the "else" would not hurt and in the function code. - Mathias.Yundt commented on April 4th 20 at 00:46
@Rhiannon_Goldner, guilty, inattentively looked code. Thought there is not a " > " and "!=" - Fausto.Kohler commented on April 4th 20 at 00:49
April 4th 20 at 00:42
If no return to write the function multiply but there remains the value but if you write return then we say the function that multiply and give me back the value don't leave at home.
April 4th 20 at 00:44
And in which language you understand the return statement?
This operator returns the value of the function.
In this case, the function getRectArea returns the product of the parameters with and height.
Really code a little with the smell.

Find more questions by tags JavaScript