简体   繁体   中英

Function returns value when called in console, but won't return when called after function declaration

I have this simple for loop that adds the numbers in a given array. As the question says, I call the function in the console and pass one of the arrays in.. no problem. Expected value is logged and returned.

But if I call the function in my script, right after my function declaration. It will complete the log but not return anything. I don't even get undefined.

I am using VS Code (can't see how that would affect anything), Running live server.

Just trying to ascertain why everything is fine when I call the function in the console, but if I call it in my script, then it refuses to return the value.

I have defined my total variable within the function, then I tried to declare it globally (not that I can see that it would make a difference on such a simple, one function script). I logged every step of the function to the console and there was no issue. Everything printed but the value was not returned.

 let numsA = [1, 10, 13, 5, 2, 1, 9, 8, 23]; let numsB = [15, 18, 5, 23, 55, 2, 9, 15, 15]; function sumArray(arr) { let sum = 0; for (let i = 0; i < arr.length; i++) { sum += arr[i]; } console.log(sum); return sum; } console.log(sumArray(numsA));

Expected Result: sumArray(numsA): 72 is logged to the console, then returned;

Actual Result: 72 is logged to the console, nothing is returned. not even undefined.

It sounds like to me you're a bit confused about what a return statement is really doing. The return statement stops the execution of a function and returns a value from that function. So after you pass an array in as an argument your function will start an execution context, it will run through your code synchronously and then when it will return sum as the functions value. the console.log will then log it to the console. Without attaching the value of the function to your console, with an alert(), or to a dom node there wont be a place for you to see what it returns.

You have to declare sum outside the function and then call the function to get what you desire.

let numsA = [1, 10, 13, 5, 2, 1, 9, 8, 23];
let numsB = [15, 18, 5, 23, 55, 2, 9, 15, 15];

let sum = 0

function sumArray(arr) {
  for (let i = 0; i < arr.length; i++) {
    sum += arr[i];
  }
}

sumArray(numsA)
console.log(sum);

The return statement is working perfectly fine in your code:

 let numsA = [1, 10, 13, 5, 2, 1, 9, 8, 23]; let numsB = [15, 18, 5, 23, 55, 2, 9, 15, 15]; function sumArray(arr) { let sum = 0; for (let i = 0; i < arr.length; i++) { sum += arr[i]; } console.log(sum); return sum; } console.log(sumArray(numsA));

As you can see, it console.log s the following:

72 
72

This is because you have two console.log calls - one inside the function:

console.log(sum);

And one outside the function:

console.log(sumArray(numsA));

The first one you would expect to work, because it's logging a variable. The second one is logging a returned value, since all operations involving calls to sumArray will result in a return value.


For example, you can create variables with returned values from functions. Here's a simple demonstration:

 function myFunction() { return "Return value"; } var returnTest = myFunction(); console.log(returnTest);

It also works with just calling a function:

 function myFunction() { return "Return value"; } console.log(myFunction());

And what the above code means is:

  • Execute myFunction
  • console.log the returned value

So when you're running, in your code:

console.log(sumArray(numsA));

It actually means:

  • Execute sumArray with the argument numsA
  • console.log the returned value

Further reading:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM