简体   繁体   中英

Creating multiple if/else conditions using javascript

I'm having an issue on how I can set multiple conditions. Basically I have three conditions that would read or listen to browser viewport. The first and second conditions are working but the third condition doesn't seem to trigger. Is it because of the conflict with the second condition?

    if($(window).width() > 1280) {
        console.log('desktop');
    }

    else if ($(window).width() < 1280) {
        console.log('tablet');
    }

    else if ($(window).width() < 780) {
        console.log('mobile');
    }

Your second condition also includes the third one. Because every value which is below 780 is also below 1280 . You need something like windowWidth < 1280 && windowWidth >= 780 to give the range.

const windowWidth = $(window).width();

if(windowWidth  > 1280) {
    console.log('desktop');
} else if (windowWidth  < 1280 && windowWidth  >= 780 ) {
   console.log('tablet');
} else if (windowWidth  < 780) {
   console.log('mobile');
}

Also it will be good to keep the window into a variable, not every time use with jQuery.

It is because of your second else/if . If $(window).width() is less than 780 , it is also less than 1280

Change it to

else if ( $(window).width() >= 780 && $(window).width() < 1280) {
    console.log('tablet');
}

Here is a more concise example, but the condition rely on the order, some people do not like this style, but I think it is OK, and we can add a comment.

//Do not change the condition order
if($(window).width() <= 780) {
    console.log('mobile');
}

else if ($(window).width() <= 1280) {
    console.log('tablet');
}

else {
    console.log('desktop');
}

Edited: The condition should be * than and equal to ....

There is flaw in your second condition...below is the fix..hope it helps!!

 if($(window).width() > 1280) {
            console.log('desktop');
        }

        else if ($(window).width() < 1280 && $(window).width() >= 780) {
            console.log('tablet');
        }

        else if ($(window).width() < 780) {
            console.log('mobile');
        }

It's because the condition before < 780 is also met (ie. If the width is equal to 600 its inferior to 1280 ). Change the condition order or, better, improve the second condition :

if($(window).width() >= 1280) {
    console.log('desktop');
}
else if ($(window).width() < 1280 && $(window).width() >= 780) {
    console.log('tablet');
}
else if ($(window).width() < 780) {
    console.log('mobile');
}

You also forgot a case, if the user's screen resolution is equal to 1280 , you'll never enter any of your conditions. I change > to >= to include that case.

you can try this also, it will satisfy your second condition

if ($(window).width() > 1280) {
  console.log('desktop');
}
else if ($(window).width() < 1280) {
  if ($(window).width() < 780) {
    console.log('mobile');
  } else {
    console.log('tablet');
  }
}

or

if ($(window).width() > 1280) {
      console.log('desktop');
    }
    else {
      if ($(window).width() < 780) {
        console.log('mobile');
      } else {
        console.log('tablet');
      }
    }

The correct way to do this would be to move your third condition up to the second spot.

When doing this kind of checks always place the most restricting condition first .

In your case, the tidiest way is:

var w = $(window).width();

if (w < 780) {               // most restrictive condition. Excludes all the others...
    console.log('mobile');
} else if (w < 1280) {       // this one includes the one above but it will never be triggered if w < 780
    console.log('tablet');
} else {                     // all the other cases
    console.log('desktop');
}

Contrary to what many said , there is no need for this else if statement:

else if (windowWidth < 1280 && windowWidth >= 780) { ... }

This adds an unnecessary, redundant check.

It sure is a light operation, but imagine that, instead of windowWidth < 1280 you were checking

functionForCalculatingWidth() : int {
   // huge amount of lines with expensive computation
   return result;
}

you would be calling the same function twice. See why it is bad design?


In the same way, if you were to check conditions based on the largest element (plus let's add another fictional condition), you would do:

var w = $(window).width();

if (w > 1280) {
    console.log('desktop');
} else if (w > 990) {
   console.log('weird device');
} else if (w > 780) {
    console.log('tablet');
} else {
    console.log('mobile');
}

Hope this will satisfy you you can check this.

Different is this shows width in console easy to understand this logic if you cant understand let me know

Here is fiddle

 $(window).bind("resize", function() { sizewindow = $(this).width(); console.log(sizewindow) if (sizewindow < 780) { console.log('mobile'); } else if (780 < sizewindow < 1280) { console.log('szechuan sauce'); if (sizewindow > 1280) { console.log('rick and morty gone for 2 years sad af') } } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM