Susan Delgado
Susan Delgado

Reputation: 140

When can I omit return in javascript

I was practicing cross browser code when I realized only one of the function in the entire util obj I made required a return and I wondered why the others do not require a return. From what I understand JavaScript will return undefined when no return is called unless it is a constructor in which case it will return this.

I tested it with all the functions in the util returning the results of each condition and it worked. Then I tested it with out returns in any of the results of the condition but it did not work without return in the getTarget function.

Can someone help me better understand the reason for this?

the jsFiddle is here http://jsfiddle.net/SusannDelgado/CnWzE/

    var evtUtil = {
    //cross browser Events
    addEvent: function (el, type, fn) {
        if (typeof addEventListener !== "undefined") {
            el.addEventListener(type, fn, false);
        } else if (typeof attachEvent !== "undefined") {
            el.attachEvent("on" + type, fn);
        } else {
            el["on" + type] = fn;
        }
    },
    removeEvent: function (el, type, fn) {
        if (typeof removeEventListener !== "undefined") {
            el.removeEventListener(type, fn, false);
        } else if (typeof detachEvent !== "undefined") {
            el.detachEvent("on" + type, fn);
        } else {
            el["on" + type] = null;
        }
    },
    getTarget: function (event) {
        if (typeof event.target !== "undefined") {
            return event.target;
        } else {
            return event.srcElement;
        }
    },
    preventDefault: function (event) {
        if (typeof event.preventDefault !== "undefined") {
            event.preventDefault();
        } else {
            event.returnValue = false;
        }
    }
};

Upvotes: 5

Views: 3309

Answers (2)

Eddie Monge Jr
Eddie Monge Jr

Reputation: 12730

You only need to declare return when you want the function to have an actual return value, whether it be a string, boolean, object, function, etc. If the function operates on something else, like adding an event handler or adding properties to something else, the return value is not needed. You could return this to be able to chain the method.

// An explicit return value
function getSomething() {
  return 'someValue';
}
var something = getSomething(); // something = someValue

// Operate on something else. No explicit return value
function changeSomething(something) {
  something = 'someOtherValue';
};
changeSomething(something); // something = someOtherValue

// Setting the variable to the return of a non-explicit return value
something = changeSomething(something); // something = undefined

// Return this to chain
var hash = {
  addProp: function (name, value) {
    this[name] = value;
    return this;
  }
};

hash.addProp('test', 'value').addProp('test2', 'value2'); // hash = Object {addProp: function, test: "value", test2: "value2"}

Upvotes: 5

n00dle
n00dle

Reputation: 6043

Return is used to pass something out of the function to the code that called it (generally speaking). Therefore in a function that doesn't need to pass anything back, such as removeEvent which is simply modifying the state of something else, it's not necessary.

The difference with getTarget is that as it says in the name, you're trying to retrieve a thing (in this case the event's target object). So in the code that calls getTarget, "something" is expected. As you say, without the return, JS simply passes back undefined.

Upvotes: 6

Related Questions