Working methods with return in JavaScript

There are some tricks for working with return that simplify the code. Consider, for example, the following code:

function func(num) { let res; if (num >= 0) { res = Math.sqrt(num); } else { res = 0; } return res; } console.log(func(3));

As you can see, in this code, depending on the condition, either one or the other value will fall into the variable res. And in the last line of the function, the content of the variable res is returned through return.

Let's rewrite this code in a shortened form, getting rid of the variable res that is unnecessary here:

function func(num) { if (num >= 0) { return Math.sqrt(num); } else { return 0; } } console.log(func(3));

Given the following function:

function func(num1, num2) { let res; if (num1 > 0 && num2 > 0) { res = num1 * num2; } else { res = num1 - num2; } return res; } console.log(func(3, 4));

Rewrite it in a shortened form according to the theory studied.

enru