2

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.

  • No and use === instead of == if it is a string. There may be a problem with incorrect string parsing. – theshadowmonkey Mar 18 '13 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 Mar 18 '13 at 18:24
  • The variable itself cant equal both "something" and "Something". But, it can equal "something" OR "Something" `if (a=="something" || a=="Something")` –  Mar 18 '13 at 18:25
  • in its most basic form http://jsfiddle.net/Rd5Ly/ –  Mar 18 '13 at 18:28

4 Answers4

3

No, but just use toLowerCase:

if (a.toLowerCase() === 'something') {
    // something here
}
Vinay
  • 6,204
  • 6
  • 38
  • 55
  • Thank you. Does the amount of "=" in the statement make a difference because your code only works how I want it to when only two "=" are used. Anyway, I feel yours is the best answer as it works with all variations on something. E.g. SoMething, with the capitalized m. –  Mar 18 '13 at 18:38
  • `===` makes sure that the type is the same as well (so both `a` and `'something'` are strings). `==` doesn't check type. – Vinay Mar 18 '13 at 18:42
2

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)

Moritz Roessler
  • 8,542
  • 26
  • 51
  • thanks. the `||` will definitely come in useful in the future and your explanation was very helpful. I didn't rate yours as best answer as it doesn't cover all variations on something (which letters are uppercase) and I imagine I would have to do it manually. It's my fault for not explaining this in my question. However, when I'm able to I'll rate your answer up. –  Mar 18 '13 at 18:45
  • You're welcome =) I'm glad enough that this will help you in the future. Thanks, I appreciate that =) , though you don't have to bother coming back here =) I'm here, more for the sake of learning and helping others out than for rep, anyway. I wish you the best for your future as a programmer, keep learning =) – Moritz Roessler Mar 18 '13 at 18:55
1

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

  if(a === "something" || a === "Something"){
Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
0

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 combination of case, look here for more about case insensitive comparison)
And as other answers says : You are probably looking for || operator.

Community
  • 1
  • 1
benzonico
  • 10,635
  • 5
  • 42
  • 50