Adorevoy
Adorevoy

Reputation: 41

What does Return do in here?

I have problem with 'return' means in this code.

1.

function func4() {
  var str = "function works.";
  console.log(str);
}
func4();

2.

function func4() {
  var str = "function works.";
  return str;
}
var value = func4();
console.log(value);

Both of them, their result is 'function works.'. I know that return used for exit function but I'm still confuse when I have to use return exactly. Sorry about my super basic question :(

Upvotes: 1

Views: 80

Answers (6)

Mohiuddin
Mohiuddin

Reputation: 90

function func4(){
            var str = "function works.";
            return str;
        }
        var value = func4();
        console.log(value);

//here return means you are returning the value of variable 'str'.
You can find the details here.

https://learn.microsoft.com/en-us/cpp/c-language/return-statement-c?view=vs-2019#:~:text=A%20return%20statement%20ends%20the,value%20to%20the%20calling%20function

Upvotes: 0

Ankit Garg
Ankit Garg

Reputation: 149

Let me try to explain it with some requirements.

  1. I want a function which returns me some data instead of passing a variable and updating the variable in function.

  2. You call a function and it is always best to test negative scenarios first. So in case of negative scenario you can return it from there it self.

In your second case if you see you are getting a value from that function and then printing it. Same thing you can not do using first function.

Always there are workarounds for everything. In the end it depends on your need and what is best suited for that situation.

Upvotes: 1

Alvaro Valenzuela
Alvaro Valenzuela

Reputation: 31

Let's analize this two scenarios:

  1. You have a function that initialize a variable with a predefinided value, and then, you log the value. Then, outside the function you execute it

  2. You have the same variable but with the difference that instead of loggin the value inside the function, you returned it from it. So you can initialize the funcion and store the value on another variable var value = func4();.

Upvotes: 2

Digvijay
Digvijay

Reputation: 8927

If you want to use the func4() value for further calculations without calling it again, then you would return {value}.

For e.g

function func4(userInput) {
    return userInput % 2 == 0;
}


var response = func4(userInput);

if(response == true) {
    console.log('user entered an even number');
} else {
    console.log('user entered a odd number');
}

// from here you can use the value of response n times without calling the function again.

Whereas, if you don't return then you will have to call the function x number of times whenever you want to re-user the response of it.

Upvotes: 0

Lewis
Lewis

Reputation: 4595

Both of those functions don't equal the same thing, but they do log the same string.

func4() in #1 is equal to undefined, because it returns nothing.

func4() in #2 returns (gives back) the value "function works.", a string, which is then given to console.log outside of the function.

function func1() {
  var str = "function works.";
  // console.log(str);
}

func1();

function func2() {
  var str = "function works.";
  return str;
}

// console.log(func2());

console.log(func1() === undefined);
console.log(func2() === 'function works.');

Upvotes: 0

Andrzej Dzirba
Andrzej Dzirba

Reputation: 356

As far as I understand 'return' assigns value to a function and returns it, so you're displaying function's value. In the first case you are just simply invoking a function to display a string.

Upvotes: 2

Related Questions