designerdre101
designerdre101

Reputation: 813

Javascript return false in if statements

Is it good practice to use "return false;" to basically say do nothing in an if statement? For example:

if (navigator.userAgent.match(/iPad/i) != null) {
    return false;
} else {
    //Usual script here
}

just wondering if there are any downfalls to this. I can use the if statement without the else but i'm just wanting to get insight on this. I have a plugin that i do not want running on iPad and so I'm wrapping it in the conditional. any comments would be appreciated!

Upvotes: 9

Views: 37201

Answers (6)

I agree with snkmchnb, otherwise just negate the condition. You can negate long expression using these:

!(a && b) = !a || !b
!(a || b) = !a && !b

And use these several times to get what you want. For example, negating a long expression

!( (a && b || c) && (d || e) || f) =
    !((a && b || c) && (d || e)) && !f =
    (!(a && b || c) || !(d || e)) && !f =
    (!(a && b) && !c || !d && !e) && !f =
    ((!a || !b) && !c || !d && !e) && !f

This looks ugly now, but negating most times doesn't mean over-complicating. For example negating "<=" results in ">"

So never use !(long_expression) neither:

if (long expression)
{
}
else
{
  //do stuff here
}

Upvotes: 0

RobertPitt
RobertPitt

Reputation: 57258

firstly it is very good practise, take this example

var window.__page_loaded__;
var Loadpage = function ()
{
    if(window.__page_loaded__ != undefined)
    {
         return; //The page has already laoded
    }

    //Proceed to load the page
}

by using the return; you doing the same as you would with an else statement but without the extra block, and as Loadpage() would normally not return any data its perfectly fine to short cut your code.

Upvotes: 1

epascarello
epascarello

Reputation: 207501

Group 1 will say it is horrible practice since it is hard to follow.

Group 2 will say do it.

Group 3 will say do it, but in 1 line

Group 4 will say do not use the else

Group 5 will say to do not use the return, just use the if around the code you want to run. AKA:

if (navigator.userAgent.match(/iPad/i) === null) {
    //Usual script here
}

Upvotes: 10

Nick Craver
Nick Craver

Reputation: 630349

You can actually simplify it further, like this:

if (navigator.userAgent.match(/iPad/i) != null) return false;
//Usual script here

Is it "good practice"...sure, if it works for you and your team. Jumping out of a function as soon as you have nothing more to do there is a very efficient way to do things, as long as it's easy to understand and maintain for whoever is working on it.

Whether you want false specifically depends on the situation, for example if you want to return but not prevent other event handlers from running later, you may want return true; instead.

Upvotes: 4

Vivin Paliath
Vivin Paliath

Reputation: 95488

You should only return a value if a caller is going to do something with that value. If you want to do "nothing" in an if statement, it's a sign that your logic is wrong. Change your statement to this:

if (navigator.userAgent.match(/iPad/i) == null) {
    //Usual script here
}

This way you don't need to "break" out of your function with the return (not a good practice in this scenario).

Upvotes: 0

MRR0GERS
MRR0GERS

Reputation: 654

In my experience only if you were actually looking for the false to be returned.

Upvotes: 5

Related Questions