admin管理员组

文章数量:1406950

I've tried looking around on here for this, but I'm not quite sure whether I'm phrasing it correctly.

if (a=="something" && a=="Something") {
//some code
}
else {
// some code
}

Can the a variable in this statement equal both "something" and "Something" with the capital S. I've tried just using "something" without the capital, however this doesn't work.

I've tried looking around on here for this, but I'm not quite sure whether I'm phrasing it correctly.

if (a=="something" && a=="Something") {
//some code
}
else {
// some code
}

Can the a variable in this statement equal both "something" and "Something" with the capital S. I've tried just using "something" without the capital, however this doesn't work.

Share Improve this question asked Mar 18, 2013 at 18:21 user2166577user2166577 4
  • No and use === instead of == if it is a string. There may be a problem with incorrect string parsing. – theshadowmonkey Commented Mar 18, 2013 at 18:22
  • a can't be equal to both "something" and "Something", which is what you are testing for, that if condition will always be false. What are you trying to do? – Chris Cherry Commented Mar 18, 2013 at 18:24
  • The variable itself cant equal both "something" and "Something". But, it can equal "something" OR "Something" if (a=="something" || a=="Something") – user1120369 Commented Mar 18, 2013 at 18:25
  • in its most basic form jsfiddle/Rd5Ly – user2166577 Commented Mar 18, 2013 at 18:28
Add a ment  | 

4 Answers 4

Reset to default 3

No, but just use toLowerCase:

if (a.toLowerCase() === 'something') {
    // something here
}

This will never work, as a can never hold 2 Strings at the same time.

What you are eventually looking for is ||

if (a=="something" || a=="Something") {
//some code
}
else {
  // some code
}

a logical OR, which means that a can either be "something" OR "Something"

What you are doing right now is checking if a is "something" AND "Something" at the same time (which is not possible)

Not at the same time. You probably want an OR.

  if(a === "something" || a === "Something"){

What you can do is check your variable ignoring case like this :

a.toUpperCase() === "SOMETHING";

Note this will work with "SoMeThinG" as well (and every bination of case, look here for more about case insensitive parison)
And as other answers says : You are probably looking for || operator.

本文标签: javascriptCan a variable equal two things in an if statementStack Overflow