Declare Variables And Return Value In One Statement In Javascript
In many functions I have noticed the following pattern: function declares variables, combines it to the result and returns result. It is shown in this very simple example: function
Solution 1:
Maybe this works for you, which is not advisable because of using assignment alogn with the comma operator.
functionfn(a, b) { // declaration in local scopereturn a = 1, b = 2, a + b; // return value with comma operator
}
A newer approach takes default values and returns just the addition of both.
fn = (a = 1, b = 2) => a + b;
Post a Comment for "Declare Variables And Return Value In One Statement In Javascript"