jeffreyveon jeffreyveon - 7 days ago 6
Javascript Question

Using 'return' instead of 'else' in JavaScript

I am working on a project which requires some pretty intricate JavaScript processing. This includes a lot of nested

if
-
else
s in quite a few places. I have generally taken care to optimise JavaScript code as much as possible by reading the other tips on Stack Overflow, but I am wondering if the following two constructs would make any difference in terms of speed alone:

if(some_condition) {
// process
return ;
}

// Continue the else condition here


vs

if(some_condition) {
// Process
}

else {
// The 'else' condition...
}

Answer

I always go with the first method. Easier to read, and less indentation. As far as execution speed, this will depend on the implementation, but I would expect them both to be identical.

Comments