2

I am reading a very comprehensive javascript programming book. In the "functions" section of language basics I have come to the following:

"The return statement can also be used without specifying a return value. When used in this way, the function stops executing immediately and returns undefined as its value. This is typically used in functions that don’t return a value to stop function execution early, as in the following example, where the alert won’t be displayed:"

function sayHi(name, message) {
return;
alert(“Hello “ + name + “, “ + message); //never called
}

I am trying to understand why anyone would want to do this. Why write a function that returns "undefined"? I have tried googling, and searching SO, but have not had much luck though this may be because I am not phrasing my search correctly as I am learning js.

Can anyone give me a real-world example of where this might be useful so that I can understand?

11
  • 1
    I think the example is meant to illustrate how return works, not that someone would ever write a function as pointless as that one. Commented May 7, 2014 at 20:18
  • 1
    Basically: the function is disabled while not creating javascript errors. Commented May 7, 2014 at 20:19
  • @j08691 OK thanks. It seemed pointless to me, but the book seemed to imply that it would be used. Commented May 7, 2014 at 20:19
  • 1
    That function is useless, that doesn't mean returning with no values is... you can use return to escape for-loops for example. If you have a massive for-loop, and you have already done what you need with it, you can escape it with return. Commented May 7, 2014 at 20:21
  • @Tim Vermaelen - so I could add "return;" before the function's statement for the purpose of debugging rather than commenting the function. I guess that is kind of useful. Commented May 7, 2014 at 20:22

6 Answers 6

6

Usually it's a conditional return. Something like

function calculateSomething(obj, condition) {
   if (condition !=0 ) return;

   obj.data = obj.data * 42;
}

in this case if some condition fails - function exits right away. Otherwise data in a passed object is modified.

Sign up to request clarification or add additional context in comments.

3 Comments

Also a great example.
That is a great example. I understand how this could be used to escape a function if a condition is met.
Only problem: if you start using jslint or jshint, the return statement has to appear in every code block (if else for while ...), therefor in this example it makes more sense to do if (condition){ obj.data... }
2

The function you provide is useless...

However, not every function needs to return something. Sometimes a function does things "elsewhere" and the value that is returned is irrelevant.

Comments

1

The function may have side effects, such as altering the state of the page. Then, the caller may not expect any return value.

2 Comments

Is there a case where placing "return;" before the function's statement would not cause the function to perform no action and return "undefined"?
If you mean a function that unconditionally returns undefined and does nothing else, then yes, it's nearly useless.
1

In my opinion, learned from masters so to speak, the idea here is to only temporarily disable the function. So if you have like an x amount of function calls to sayHi() like for example in a repeater or spread out over multiple files, the idea can be useful to simply return; without a value. Useful? Yes, to avoid commenting out chunks of code, but that's it.

Leaving this in a development environment, which initially will enter the www, you should not write it like this, and always make sure that: when a function has to return "something", then "something" always counts for value, no matter what the condition. With lowest value a boolean => return false;. This counts for every code block inside that function => for if else while ...

/* INCORRECT: it only returns something conditionally */
function do(something){
    if (condition){
        return false;
    }
}

/* CORRECT: it returns value no matter what the condition */
function do(something){
    if (!condition){
        return true;
    }

    return false;
}

/* OPTIMIZED: a lot of ideas can be optimized, especially when returning booleans */
function do(something){
    return !condition;
}

The rules only apply if you want to write proper code. Using tools like jslint, jshint or re-sharper helps a lot in understanding the basic principles of writing ecmascript valid code. Since not everyone is aware of these rules, all examples will yield the same result. In the sens of "it works, but it's not valid code".

Comments

0

Because A JS function is an object(has:Attributs+methods)

And , An object should have a value which can be undefined .

The proof is that

function sayHi(){}

Can be handled such as object even calling it :

var returnedValue=sayHi.call() ;

If sayHi does not return anything, returnedValue will be undefined

Comments

0

Example if you want to use classes I am sure about it you will use setter functions to give value for an instance attribute.

Like this:

function Something(val) {
    this.value = val;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.