开发者

When to use return, and what happens to returned data?

开发者 https://www.devze.com 2023-03-30 09:20 出处:网络
What is the difference between: function bla1(x){console.log(x)} and function bla(x){return console.log(x)}

What is the difference between:

function bla1(x){console.log(x)}

and

function bla(x){return console.log(x)}

In which cases should I use re开发者_JS百科turn?

also, when a value is returned from a function, what happens to it? is it stored somewhere?


What is the difference

The first function returns undefined (as it does not return anything explicitly), the second one returns whatever console.log returns.

In which cases should I use return?

When the function is generating some value and you want to pass it back to the caller. Take Math.pow for example. It takes two arguments, the base and the exponent and returns the base raised to the exponent.

When a value is returned from a function, what happens to it? is it stored somewhere?

If you want to store the return value, then you have to assign it to a variable

var value = someFunction();

This stores the return value of someFunction in value. If you call the function without assigning the return value, then the value is just silently dropped:

someFunction();

These are programming basics and are not only relevant to JavaScript. You should find a book which introduces these basics and in particular for JavaScript, I recommend to read the MDN JavaScript Guide. Maybe the Wikipedia article about Functions is helpful as well.


Return in a function is a way to pass back data from the function.

Example:

function test(){
   var test = 1+1;
   return test;
}

var feedback = test(); //feedback would now contain the value 2 if outputted.

We could also send a variable into the function and then return it back out.

Example 2:

 function test(i){
    i= i+1;
    return i;
 }

 var feedback = test(1); //feedback would also output the value 2.


As you already mentioned, return gives you the possibility to call a function and save its returning value.

Here is a little example:

function bla(x) { return "blablabla"; }

If I call the method I will get a string with blablabla:

var blastring = bla(x);
alert(blastring);

This would result in an alert with blablabla.


With return you specify what the value of a function is. You can use this value to do further operations or to store it into a variable and so on.

Since console.log returns undefined, the examples in your question are equivalent, as a function not reaching a return statement will return undefined as well. But let me give you an example:

function sum(arr) {
    var s = 0;
    for (var index in arr) {
        s += arr[index];
    }
    return s;
}

function prodsum(arr, scalar) {
    return scalar * sum(arr);
}

console.log(prodsum([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3));

The result will be 165. If we remove the returns, then both functions will return undefined:

function sum(arr) {
    var s = 0;
    for (var index in arr) {
        s += arr[index];
    }
    s;
}

function prodsum(arr, scalar) {
    scalar * sum(arr);
}

console.log(prodsum([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3));

and the result will be undefined as well. Basically, if you want the function to have a conclusion or final value, then you have a return in it.


If you just add return; in the function. It stops the execution of the function.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号