Should I always return a value in javascript functions?

Yes and no.

If you don't return something, you will return undefined by default (note that you can't assume this to be true in all languages. Perl, for example, returns the last vivified value).

So, not returning something from a function is the same as returning undefined. Which leads to the inference that there's no need to return anything:

function x() {                   }
function y() { return;           }
function z() { return undefined; }

x() === y(); //  is true
x() === z(); //  is true"

No comments:

Post a Comment