1

Is there a way to have multiple javascript "if" statements in one? for example:

if (location.host === "www.google.com", "www.jsfiddle.net") { blah };

or would I just have to use "||", example:

if (location.host === "www.google.com" || location.host === "www.jsfiddle.net"){ blah };

If there is a way to do this, or perhaps an easier way, could you please note it below.

This is different because the other question has only received JS answers, however this one has received jQuery replies, as well as it is to obtain different goals.

user3392846
  • 65
  • 1
  • 7
  • _"or would I just have to use "||""_ Yes, or create an array and see if the element exists in it. – j08691 Apr 19 '15 at 03:54
  • 2
    `if (location.host in {'www.google.com':1, 'www.jsfiddle.net':1}) { /* blah */ }` – nnnnnn Apr 19 '15 at 04:19
  • @JarrodRoberson This is not a duplicate of that question. I would say this is subset of that questions. Just have a look at the answers in this question and that question, mostly trying to achieve a different goal. – Farid Nouri Neshat Apr 19 '15 at 04:42
  • `["www.google.com", "www.jsfiddle.net"].some(Object.is.bind(0, "www.google.com"))` – dandavis Apr 19 '15 at 04:52

6 Answers6

4

You can use javascript indexOf method in this way:

var array = ["www.google.com", "www.jsfiddle.net"]
if(array.indexOf(location.host) != -1)
{ //ok
}
hamed
  • 7,939
  • 15
  • 60
  • 114
3

Another option would be to use a switch case. My personal practice is that if I have to perform more than one elseif statement I use a switch case. IMO its simply easier to read and if you start to deal with really large lookups its more performant.

switch(location.host) {
  case 'www.google.com':
    // blah
    break;
  case 'www.jsfiddle.net':
   //blah
   break;
  case 'www.stackoverflow.com':
   // blah
   break;
  default:
   // blah
}
Scott Sword
  • 4,648
  • 6
  • 32
  • 37
1

The idiom is:

if(["www.google.com", "www.jsfiddle.net"].indexOf(location.host) > -1) { ... }

But this will only work in IE9 and above, as well as modern browsers.

Interrobang
  • 16,984
  • 3
  • 55
  • 63
1

Not with the syntax you have, however, you could write a function.

function assertAnyEquals(value) {
      "use strict";
      var args = new Array(arguments.length);
      for (var i = 1; i < arguments.length; i++) { 
          args[i] = arguments[i];
      } 
      console.log(args);

      return args.some(function(arg) { 
        if (value === arg) { 
          return true;
        }
      });
    }
console.log(assertAnyEquals("v", "c"));

Then the use would be:

if (assertAnyEquals(location.href, "www.google.com", "www.jsfiddle.net")) { 
    // do stuff 
}

I prefer something like this because it's obvious what I'm doing the name of the function.

Edit: Updated the implementation of this, because I realized it iterated through array elements even after a match was found. Now it doesn't.

jdphenix
  • 15,022
  • 3
  • 41
  • 74
  • Avoid `Array.prototype.slice.call`; better to just use a `for` loop here. https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#32-leaking-arguments – Jackson Apr 19 '15 at 04:31
1

indexOf doesn't make much sense in terms of readability. I prefer using a contains function to encapsulate the "existence checking". The Lodash and Underscore libraries have one, but you could just as easily roll your own:

function contains(array, element) {
    return array.indexOf(element) > -1;
}

if (contains(["www.google.com", "www.jsfiddle.net"], location.host)) { blah };
Jackson
  • 9,188
  • 6
  • 52
  • 77
1

i like using Object lookup tables, because it can saves quotes but always executes much faster than searching via indexOf:

var oks= {"www.google.com":1, "www.jsfiddle.net":1}
if(oks[location.host]===1){ 
   alert("ok!")
}

it's much faster because there's no method call and property lookup is very fast. it's even faster than switch on large collections, and certainly more flexible and re-usable.

anytime i need one value from another value, without nested conditions or other complications, i go for the LUT.

this pattern also allows you to easily add new values (oks[key]=1), use extend() to merge collections without dupes, and semantically remove un-needed choices (delete oks['www.google.com']), instead of a bunch of splice(indexOf(),1) malarky. it also works great in old browsers, unlike [].indexOf()...

you can use hasOwnProperty.call(oks, key) as well, but it negates some performance benefits.

also, this is really only good for functions, strings, numbers, and some arrays, it won't allow lookup by objects, like a Map() would.

i should mention there's also another IE8-friendly way of doing indexOf: if(",www.google.com,www.jsfiddle.net,".indexOf([,key,])!==1){...}

dandavis
  • 16,370
  • 5
  • 40
  • 36
  • You should probably use `oks.hasOwnProperty(location.host)` in case the value you are checking is the string `"hasOwnProperty"`, `"valueOf"` etc. Also, this will only work for strings and numbers. – Jackson Apr 19 '15 at 04:24