function

Should functions always return something (Javascript)


Should functions always return something? I often write very basic functions which are used as shorthand to do things that happen a lot such as:

function formsAway() {
    $("#login_form, #booking_form").slideUp();
}

Should this function exist - is there a better way, or is this fine?


Solution

  • They don't have to return anything. If you leave it blank it simply returns 'undefined' which in this case is fine because you never intend to use the return value. The Javascript syntax is pretty simplistic and as far as I know there just isn't any real distinction between functions that do and functions that don't return a value (other than the 'return' keyword)