I am trying to get 3 conditions in one if . But it's not working

I need to match three conditions so that the page can show the appropriate result for that filters. I tried adding the && between three of the conditions but it doesn’t work.
I am not revealing the real code here, but I’ve shown it without the original var names.

if(localStorage.getItem("eg1") == 30000 &&  eg2 == 1 &&   eg3 == use){};

eg2 and eg3 are just variables, they are not localstorage.

5 thoughts on “I am trying to get 3 conditions in one if . But it's not working”

  1. Try to decompose the problem in order to analyze it better:

    var e1 = localStorage.getItem("eg1") === 30000;
    var e2 = eg2 === 1;
    var e3 = eg3 === use;

    I’ve used triple equal since i suppose you don’t want type coercion.

    Since you can’t show the original code, if you don’t put some log of your results it’s quite difficult to imagine what variables can contain with appropriate level of certainty.

    Sure is that looking to your conditions independently should help you to identify where your guesses fall to fix what appears as a simple branch condition.

  2. In your case I think the conditions must be if(localStorage.getItem("eg1") == 30000 && localStorage.getItem("eg2") == 1 && localStorage.getItem("eg3") == use){}

  3. Here is a bit cleaner way to do it:

    const a = ["eg1", "eg2", "eg3"].map(e => localStorage.getItem(e));
    if (a[0] == 30000 && a[1] == 1 && a[2] == use) {

    And a less dynamic but shorter way to do it:

    if ([30000, 1, use].map((e, i) => e == localStorage.getItem("eg" + i++)).includes(false)) {

Leave a Comment