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
4 Answers
Reset to default 3No, 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
版权声明:本文标题:javascript - Can a variable equal two things in an if statement? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745050438a2639620.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论