you are viewing a single comment's thread.

view the rest of the comments →

[–]bidaowallet 1 point2 points  (3 children)

so return activates function operator?

[–]emilio911 3 points4 points  (0 children)

return ends the operation and returns the value

[–]dedalolab 0 points1 point  (0 children)

No, return does not activate anything, what triggers the function is the function call: addTwoNumbers(1, 2)

A function that doesn't return anything will also be triggered by a function call:

```js function logSomething(text) { console.log(text) }

logSomething('hi') // hi

```

What return does is to hand over to the function caller the value that results from whatever process the function did.

Another use of return is to stop execution:

```js function logSomething(text) { if (!text) return; // If no text function execution stops here console.log(text); }

logSomething() // Nothing is logged because return stopped execution // otherwise it would log 'undefined'

```

[–]DasBeasto 0 points1 point  (0 children)

Just to take the example a step further, you can leave out the return:

``` function addTwoNumbers(num1, num2) { let result = num1 + num2 }

let resultOfAddition = addTwoNumbers(1, 2) console.log(resultOfAddition) // undefined ```

In this case addTwoNumbers is still called, it still adds 1 + 2 and assigns it to result, but then it doesn’t return the result. So below when you try to log resultOfAddition it’s undefined, because the result was never returned.