4

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 
} 
Sam Assoum
  • 437
  • 1
  • 8
  • 21
  • 2
    You use it wrongly, it will be: ```if ( dayOfWeek == 4 || dayOfWeek == 5 || dayOfWeek == 6 && hour >= 17 && hour < 19 ) {``` – Matteo Gaggiano Jun 30 '17 at 07: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 Jun 30 '17 at 07:37
  • 1
    Restate the condition for each condition! – Andrew Li Jun 30 '17 at 07:38
  • @briosheje - What made you think of `Array#indexOf` ? #Amazing – Rayon Jun 30 '17 at 07:39
  • @Rayon: it's easier and faster to read. Also, it's more mantainable if you ever think of having the "sourcedays" being generated dinamically (either from an external source, either from a calculation). I'm rather wondering whether I'm the only one who though about this, I hate to write 3 condition statements when they can be condensed in a single line :P – briosheje Jun 30 '17 at 07:41
  • 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 Jun 30 '17 at 07:45
  • @briosheje - Yeah! Good to have shorter and no-repetitive code :) – Rayon Jun 30 '17 at 07:47
  • Possible duplicate of [Or operator not working in IF statement Node.js](https://stackoverflow.com/questions/33089632/or-operator-not-working-in-if-statement-node-js) – user202729 Jul 30 '18 at 06:59

6 Answers6

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 ) {
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
3

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
}
grpcMe
  • 1,367
  • 5
  • 15
  • 28
3

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/

briosheje
  • 7,356
  • 2
  • 32
  • 54
2

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");
}
Durgpal Singh
  • 11,481
  • 4
  • 37
  • 49
1

Your if condition should be:

if ( (dayOfWeek == 4 || dayOfWeek == 5 || dayOfWeek == 6) && hour >= 17 && hour < 19 ){
    // do stuff
  } else {
    // do other stuff 
} 
blueren
  • 2,730
  • 4
  • 30
  • 47
1

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")
}
Jeet
  • 185
  • 1
  • 2
  • 10
  • The `(dayOfWeek == 4 || 5 || 6)` won't work correctly, it will always return true (see operator precedence https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence). First the `==` is evaluated (which returns `true` only if `dayOfWeek = 4`), if it returns `false` the whole expression returns `5` which when evaluated as Boolean is true. So no matter the value of `dayOfWeek` you will always get `true` from `(dayOfWeek == 4 || 5 || 6)`. – f1ames Jun 30 '17 at 07:59
  • The code is essentially same as of what has been posted in question, just with few parentheses. – abhishekkannojia Jun 30 '17 at 08:08