Jump to content
Larry Ullman's Book Forums

Creating A Utility Library - Return True


Recommended Posts

Hi,

 

I have one simple question about creating a utility library:

 

 

var U = {
    $: function(id) {
        'use strict';
        if (typeof id == 'string') {
            return document.getElementById(id);
        }
    }
    setText: function(id, message) {
        'use strict';
        if ((typeof id == 'string') && (typeof message == 'string')) {
            var output = this.$(id);
            if (!output) return false;
            if (output.textContent !== undefined) {
                output.textContent = message;
            } else {
                output.innerText = message;
            }
            return true;
        }
    }

.

.

.

.

 

Is it necessary to return true, and why this method must return true ?

 

Thank you in advance :)

 

 

Link to comment
Share on other sites

Note that true is returned only when the first if statement in the function is satisfied.

If that if statement is not satisfied, then true is not returned, and implicitly, at the end of the function, JS will return undefined (which loosely evaluates to false).

As such, you can check the return value to know whether or not the first if statement was satisfied or not.

Link to comment
Share on other sites

Note that true is returned only when the first if statement in the function is satisfied.

If that if statement is not satisfied, then true is not returned, and implicitly, at the end of the function, JS will return undefined (which loosely evaluates to false).

As such, you can check the return value to know whether or not the first if statement was satisfied or not.

Thank you HartleySan. If I understood correctly - that is just good practice (because of debugging)... But let's say that there is no that line: return true;. If the first statement in the function is satisfied - JS function will not return undefined (which loosely evaluates to false), right? If so, then why return true?

Link to comment
Share on other sites

If the return true line is not in the code, then the function will always return undefined no matter what.

Whenever a return statement is encountered, the function is immediately exited, and nothing after the return statement is executed.

However, if there is no return statement in a function, then when the end of the function is reached, JavaScript implicitly returns undefined from the function.

 

In other words, without return true, you have no way of knowing whether the first if statement was satisfied or not because undefined would always be returned.

  • Upvote 1
Link to comment
Share on other sites

If the return true line is not in the code, then the function will always return undefined no matter what.

Whenever a return statement is encountered, the function is immediately exited, and nothing after the return statement is executed.

However, if there is no return statement in a function, then when the end of the function is reached, JavaScript implicitly returns undefined from the function.

 

In other words, without return true, you have no way of knowing whether the first if statement was satisfied or not because undefined would always be returned.

Yes, my mistake, I need to check (test) before I ask... ^_^

 

23r1hy0.png

 

Thank you very much!

Link to comment
Share on other sites

Hi, 

 

   Just stumbled on this thread. I'm curious - WHY would you add the 'return:true' to your utilities.js? It's not in Larry's original - what impact would this have on the overall impact once it's integrated into the page? 

 

   thanks, 

 

   Tim

Link to comment
Share on other sites

Similar to what we discussed above, it allows you to differentiate what was achieved in the function based on the return state.

 

Specifically, if both the id and message parameters are strings, then true will always be returned by the function. On the other hand, if one or the other (or both) are not strings, then undefined will be implicitly returned at the end of the function.

By checking the return value (i.e., true or undefined), you can then determine whether the id and message parameters were strings or not, which may assist in debugging or any number of things.

 

Again, it really all depends on what you're trying to accomplish, but in general, the return true is simply used to determine how far along the function got based on the provided argument(s).

 

That make sense?

Link to comment
Share on other sites

 Share

×
×
  • Create New...