最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

if statement in javascript always true - Stack Overflow

programmeradmin0浏览0评论

So, I have the code, its not done, but all i want it to do is display one alert box if I write the word 'help', and say something else if anything else is entered.

function prompter() {
var reply = prompt("This script is made to help you learn about new bands, to view more info, type help, otherwise, just click OK") 
if (reply === 'help' || 'Help')
  {
  alert("This script helps you find new bands. It was originally written in Python 3.0.1, using Komodo IDE, but was then manually translated into Javascript. Please answer the questions honestly. If you have no opinion on a question, merely press OK without typing anything.")
  }
else
  {
  alert("Press OK to continue")
  }
};

but, what happens, is no matter what, the first alert box pops up, even if you press cancel! How should I fix this???

So, I have the code, its not done, but all i want it to do is display one alert box if I write the word 'help', and say something else if anything else is entered.

function prompter() {
var reply = prompt("This script is made to help you learn about new bands, to view more info, type help, otherwise, just click OK") 
if (reply === 'help' || 'Help')
  {
  alert("This script helps you find new bands. It was originally written in Python 3.0.1, using Komodo IDE, but was then manually translated into Javascript. Please answer the questions honestly. If you have no opinion on a question, merely press OK without typing anything.")
  }
else
  {
  alert("Press OK to continue")
  }
};

but, what happens, is no matter what, the first alert box pops up, even if you press cancel! How should I fix this???

Share Improve this question edited Feb 15, 2012 at 23:37 Timothy Jones 22.1k6 gold badges64 silver badges94 bronze badges asked Feb 15, 2012 at 23:24 BilljkBilljk 10.7k23 gold badges56 silver badges73 bronze badges 0
Add a comment  | 

5 Answers 5

Reset to default 12
if (reply === 'help' || 'Help')

should be:

if (reply === 'help' || reply === 'Help')

since 'Help' is "truthy" and so the first part of the if will always be entered.

Of course, even better would be to do a case-insensitive comparison:

if (reply.toLowerCase() === 'help')

Example: http://jsfiddle.net/qvEPe/

The problem is here:

if (reply === 'help' || 'Help') // <-- 'Help' evaluates to TRUE
                                //      so condition is always TRUE

The equality operator doesn't "distribute", try

if (reply === 'help' || reply === 'Help')

The reason why it always pops up is that reply === 'help' || 'Help' evaluates as (reply === 'Help') || ('Help'). The string literal Help is always truthy in Javascript hence it always evaluates to truthy.

To fix this you need to compare reply to both values

if (reply === 'help' || reply === 'Help') {
  ...
}

Or if you want any case variant of help use a regex

if (reply.match(/^help$/i)) {
  ...
}

Just change this: if (reply === 'help' || 'Help')

To this: if (reply === 'help' || reply === 'Help')

The or statement was not comparing the variable.

The problem is this line:

 if (reply === 'help' || 'Help')

Because in JavaScript, objects and non-empty strings evaluate to true when used as a boolean. There are a couple of exceptions to this when using ==

 if("0") // true
 if("0" == true) // false

In general, it's not a good idea to use == or raw variables in if statements.

As others have pointed out, use

if (reply === 'help' || reply === 'Help')

Or better:

if (typeof reply === 'string' && reply.toLowerCase() === 'help')

instead.

发布评论

评论列表(0)

  1. 暂无评论