The return statement

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

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 :frowning:

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

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:

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.

1 Like

Hi Lionel , thanks for your response !

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 !!

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:

function functionName() {
  //do stuff
}

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

Here it is !!


But still it’s the same @lionel-rowe

yes @camperextraordinaire just that code

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

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

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.

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

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:

    (function() {
      var result = console.log();
      return result;
    })();
    
  • 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.

    It then returns undefined, which Chrome also prints to the console.

Hopefully that’s a bit clearer?

3 Likes