# The return statement

**URL:** <https://forum.freecodecamp.org/t/the-return-statement/152980>\
**Category:** General\
**Created:** [October 13, 2017, 1:58pm UTC](https://forum.freecodecamp.org/t/the-return-statement/152980 "2017-10-13T13:58:37Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![suryaiduri](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/suryaiduri/32/63550_2.png) [@suryaiduri](https://forum.freecodecamp.org/u/suryaiduri)\
**Post date:** [October 13, 2017, 1:58pm UTC](https://forum.freecodecamp.org/t/the-return-statement/152980/1 "2017-10-13T13:58:37Z")

</div>

Hello Everyone,  
I’m really confused about the return statement ☹

```auto
var testString = "There are 3 cats but 4 dogs.";

// Only change code below this line.

var expression = /\d+/g; // Change this line

// Only change code above this line

// This code counts the matches of expression in testString
var digitCount = testString.match(expression);

return digitCount;

```

When I return digitCount in the above statement I’m getting illegal return statement error ☹

can someone pls explain me, How to use the return statement ?

---

<div class="post-metadata">

**Author:** ![lionel-rowe](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lionel-rowe/32/41877_2.png) [@lionel-rowe](https://forum.freecodecamp.org/u/lionel-rowe)\
**Post date:** [October 13, 2017, 2:13pm UTC](https://forum.freecodecamp.org/t/the-return-statement/152980/2 "2017-10-13T14:13:39Z")

</div>

Only functions can return. A function returning a value doesn’t (on its own) “do” anything; it doesn’t make the value visible to the user in any way. However, the value can then be used for other purposes.

For example:

```javascript
function addTwoNumbers(x, y) {
  return x + y;
}

addTwoNumbers(1, 2);
// ^ the results of this will never be shown to end users
// however...

document.querySelector('#myDiv').textContent = addTwoNumbers(1, 2);

// ^ this will show the result in the element with the ID '#myDiv'.

```

In this challenge, you don’t need to worry about what is and isn’t visible to users, nor about functions or returning. All you need to do is ensure that the value of `expression` is the regex `/\d+/g` and that the value of `digitCount` is 2 (because there are 2 numbers in the string).

I also noticed that it seems like you’ve edited the code below the “Only change code above this line” comment, so now `digitCount` contains the value `['3', '4']`, which is an array of the matches rather than the _number_ of matches.

---

<div class="post-metadata">

**Author:** ![suryaiduri](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/suryaiduri/32/63550_2.png) [@suryaiduri](https://forum.freecodecamp.org/u/suryaiduri)\
**Post date:** [October 13, 2017, 4:04pm UTC](https://forum.freecodecamp.org/t/the-return-statement/152980/3 "2017-10-13T16:04:50Z")

</div>

Hi Lionel , thanks for your response !

```auto
var quotedText = /'([^']*)'/;
function check ()
{
	debugger;
	var result=console.log(quotedText.exec("She said 'hello'"));
	return result;
}
check();

```

for the above code , I’m getting check is not defined error , could you pls explain me why ??

Thanks !!

---

<div class="post-metadata">

**Author:** ![lionel-rowe](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lionel-rowe/32/41877_2.png) [@lionel-rowe](https://forum.freecodecamp.org/u/lionel-rowe)\
**Post date:** [October 13, 2017, 4:20pm UTC](https://forum.freecodecamp.org/t/the-return-statement/152980/5 "2017-10-13T16:20:41Z")

</div>

I’m speculating, but it might be because the parser is misinterpreting your function declaration due to having the first brace `{` on the next line after the function name and arguments `function check()`. Convention is usually to format similar to the following:

```javascript
function functionName() {
  //do stuff
}

```

At any rate, running that code in the Chrome browser console doesn’t throw any errors.

---

<div class="post-metadata">

**Author:** ![suryaiduri](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/suryaiduri/32/63550_2.png) [@suryaiduri](https://forum.freecodecamp.org/u/suryaiduri)\
**Post date:** [October 13, 2017, 4:22pm UTC](https://forum.freecodecamp.org/t/the-return-statement/152980/6 "2017-10-13T16:22:51Z")

</div>

Here it is !!

 ![image](https://global.discourse-cdn.com/freecodecamp/original/3X/9/7/972dfbb32c0e61722da4675c6a2b3ae5bb04d173.png)  
But still it’s the same @lionel-rowe

---

<div class="post-metadata">

**Author:** ![suryaiduri](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/suryaiduri/32/63550_2.png) [@suryaiduri](https://forum.freecodecamp.org/u/suryaiduri)\
**Post date:** [October 13, 2017, 4:41pm UTC](https://forum.freecodecamp.org/t/the-return-statement/152980/8 "2017-10-13T16:41:26Z")

</div>

yes @camperextraordinaire just that code

---

<div class="post-metadata">

**Author:** ![lionel-rowe](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lionel-rowe/32/41877_2.png) [@lionel-rowe](https://forum.freecodecamp.org/u/lionel-rowe)\
**Post date:** [October 13, 2017, 4:43pm UTC](https://forum.freecodecamp.org/t/the-return-statement/152980/9 "2017-10-13T16:43:02Z")

</div>

Are you sure it’s in a browser console not in an online editor? What’s it trying to do with `eval`?

---

<div class="post-metadata">

**Author:** ![suryaiduri](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/suryaiduri/32/63550_2.png) [@suryaiduri](https://forum.freecodecamp.org/u/suryaiduri)\
**Post date:** [October 13, 2017, 4:47pm UTC](https://forum.freecodecamp.org/t/the-return-statement/152980/10 "2017-10-13T16:47:44Z")

</div>

yup I’m in chrome console window but I just tried IIFE , I got the result. but still I’m not sure why it’s printing undefined even when I return result

 ![image](https://global.discourse-cdn.com/freecodecamp/original/3X/4/5/4556439466e44e9fc9cd090553a6401bb7854427.png)

---

<div class="post-metadata">

**Author:** ![lionel-rowe](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lionel-rowe/32/41877_2.png) [@lionel-rowe](https://forum.freecodecamp.org/u/lionel-rowe)\
**Post date:** [October 13, 2017, 4:52pm UTC](https://forum.freecodecamp.org/t/the-return-statement/152980/11 "2017-10-13T16:52:59Z")

</div>

That’s what’s returned by the function. You told it that `result = console.log(/*blah*/)`, which executes the `console.log()` action but returns `undefined`.

---

<div class="post-metadata">

**Author:** ![suryaiduri](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/suryaiduri/32/63550_2.png) [@suryaiduri](https://forum.freecodecamp.org/u/suryaiduri)\
**Post date:** [October 13, 2017, 4:54pm UTC](https://forum.freecodecamp.org/t/the-return-statement/152980/12 "2017-10-13T16:54:20Z")

</div>

But it returned some output right ?why it’s undefined  
sorry if I’m asking too much !

---

<div class="post-metadata">

**Author:** ![lionel-rowe](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lionel-rowe/32/41877_2.png) [@lionel-rowe](https://forum.freecodecamp.org/u/lionel-rowe)\
**Post date:** [October 13, 2017, 5:02pm UTC](https://forum.freecodecamp.org/t/the-return-statement/152980/14 "2017-10-13T17:02:09Z")

</div>

To clear up any confusion:

- Every function returns a value

- If there is no `return` statement, a function will return `undefined` by default

- `console.log()` is a function with no `return` statement

- Therefore, `console.log()` returns `undefined`

- A function written like this will therefore also return undefined:

- However, returning `undefined` doesn’t mean the function doesn’t _do_ anything. For example, it can still log information to the console, manipulate the DOM, display an alert, modify a variable it has access to, etc.

- In your case, the function _does_ do something — it logs the results of `quotedText.exec("She said 'hello'")` to the console.

Hopefully that’s a bit clearer?

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 17, 2021, 9:37am UTC](https://forum.freecodecamp.org/t/the-return-statement/152980/15 "2021-01-17T09:37:19Z")

</div>


