jeffreyveon jeffreyveon - 1 year ago 67
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

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


if(some_condition) {
// Process

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download