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

Javascript OR operator not working in if statement - Stack Overflow

programmeradmin3浏览0评论

I'm trying to get this Javascript to do something if the day of the week matches any of the days listed in my statement, as well as restricting it to between 17:00 and 19:00 hours, but the OR operator is not working as I expected, I'm new to JS and I'm wondering if I'm misunderstanding the use of this operator. If I were to list a value for just one day of the week, instead of 3 like in my example, the code works as I'd hoped.

var d = new Date();
var dayOfWeek = d.getDay(); // 0 = Sunday
var hour = d.getHours();

if ( dayOfWeek == 4 || 5 || 6 && hour >= 17 && hour < 19 ){
    // do stuff
  } else {
    // do other stuff 
} 

I'm trying to get this Javascript to do something if the day of the week matches any of the days listed in my statement, as well as restricting it to between 17:00 and 19:00 hours, but the OR operator is not working as I expected, I'm new to JS and I'm wondering if I'm misunderstanding the use of this operator. If I were to list a value for just one day of the week, instead of 3 like in my example, the code works as I'd hoped.

var d = new Date();
var dayOfWeek = d.getDay(); // 0 = Sunday
var hour = d.getHours();

if ( dayOfWeek == 4 || 5 || 6 && hour >= 17 && hour < 19 ){
    // do stuff
  } else {
    // do other stuff 
} 
Share Improve this question asked Jun 30, 2017 at 7:35 Sam AssoumSam Assoum 4471 gold badge9 silver badges21 bronze badges 8
  • 2 You use it wrongly, it will be: if ( dayOfWeek == 4 || dayOfWeek == 5 || dayOfWeek == 6 && hour >= 17 && hour < 19 ) { – Matteo Gaggiano Commented Jun 30, 2017 at 7:37
  • 2 dayOfWeek == 4 || 5 || 6 this is invalid in javascript, you likely want to: [4,5,6].indexOf(dayOfWeek) > -1 && hour >= 17 && hour <19 -> note that this is an approach slightly different (using an array). – briosheje Commented Jun 30, 2017 at 7:37
  • 1 Restate the condition for each condition! – Andrew Li Commented Jun 30, 2017 at 7:38
  • @briosheje - What made you think of Array#indexOf ? #Amazing – Rayon Commented Jun 30, 2017 at 7:39
  • 1 dayOfWeek == 4 == true or false. Whereas dayOfWeek == 4 || 5 will yield true or 5. Whereas dayOfWeek == 4 || 5 || 6 will still yield true or 5. It doesn't do what you want. – Stephen Quan Commented Jun 30, 2017 at 7:45
 |  Show 3 more comments

6 Answers 6

Reset to default 8

In this case, you better use a range check, because you need only two comparisons against of three or more - and it is better maintanable, just to change a value, if necessary.

if (dayOfWeek >= 4 && dayOfWeek <= 6 && hour >= 17 && hour < 19) {

The right OR conditions needs parenthesis, because of the precedence of && over ||

if ((dayOfWeek == 4 || dayOfWeek == 5 || dayOfWeek == 6) && hour >= 17 && hour < 19 ) {

You need to use dayOfWeek but you can also limit the amount of checks you need to do....

var d = new Date();
var dayOfWeek = d.getDay(); // 0 = Sunday
var hour = d.getHours();

if ( (dayOfWeek >= 4 && dayOfWeek <= 6) && (hour>=17 && hour < 19))
{
  // do stuff
}
else
{
  // doo other stuff
}

Just for the sake of posting another possibility, if you ever will have a dynamic input you may want to use an array and use indexOf to check whether the day exists in the list:

var d = new Date();
var dayOfWeek = d.getDay(); // 0 = Sunday
var hour = d.getHours();

if ( [4,5,6].indexOf(dayOfWeek) > -1 && hour >= 17 && hour < 19 ){
    // do stuff
  } else {
    // do other stuff 
}

https://jsfiddle.net/hnzzfnot/1/

var d = new Date();
var dayOfWeek = d.getDay(); // 0 = Sunday
var hour = d.getHours();

if ( (dayOfWeek == 4 || dayOfWeek == 5 || dayOfWeek == 6) && (hour >= 17 && hour < 19) ){
    // do stuff
    console.log("true");
  } else {
    // do other stuff 
    console.log("false");
}

Your if condition should be:

if ( (dayOfWeek == 4 || dayOfWeek == 5 || dayOfWeek == 6) && hour >= 17 && hour < 19 ){
    // do stuff
  } else {
    // do other stuff 
} 

Correct it like this,

var d = new Date();
var dayOfWeek = d.getDay(); // 0 = Sunday
var hour = d.getHours();

if ( (dayOfWeek == 4 || 5 || 6) && (hour >= 17 && hour < 19) ){
    console.log("if")
  } else {
    console.log("else")
}
发布评论

评论列表(0)

  1. 暂无评论