Skip to content Skip to sidebar Skip to footer

Multiple Or Strings

I cannot find out how to pass multiple strings in an If statement. Here is my Code : var date = new Date(); if (document.getElementById('postcode-entry').value == ('G74'

Solution 1:

The phrase ("G74" || "G75") forces a boolean evaluation on each string, and both will return true always.

So you would need to do something like this:

var myvar = document.getElementById("postcode-entry").value;

if(myvar === "G74" || myvar === "G75")

Solution 2:

i am not sure if you want to follow this approach but try using the following-

var strArr = [ 'G74', 'G75' ];

if( strArr.indexOf( document.getElementById("postcode-entry").value ) !== -1 ) {
// Your  normal code goes  here
}

Using this, you can have n number of string tested in a single statement inside if.

Solution 3:

This should do

var post_code = document.getElementById("postcode-entry").value;
  if (post_code == "G74" || post_code == "G75") 

Solution 4:

I have never seen this before. Perhaps you can use the switch statement

But in your case I would recomment the following:

varposcode= document.getElementById("postcode-entry").value
if(postcode === "G74" || postcode === "G75") ......

Post a Comment for "Multiple Or Strings"