Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 304
designerdre101 Avatar asked Sep 14 '10 17:09

designerdre101


2 Answers

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
}
like image 191
epascarello Avatar answered Sep 17 '22 15:09

epascarello


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

like image 29
MRR0GERS Avatar answered Sep 21 '22 15:09

MRR0GERS