-3

I try to declare bool variable and then check it, inside function, but I get error that there is unexpected identifier pointing to line where daysCreated is comparing to false

<script type="text/javascript">

var daysCreated = false;

function createDays() {
 if daysCreated == false {
    //do something
    }
    daysCreated = true;
  }
}

function createDays is being called on button click inside document.

Alexey K
  • 6,537
  • 18
  • 60
  • 118

4 Answers4

4

You forgot your parenthesis.

function createDays() {
 if (daysCreated === false) {
    //do something
  }
  daysCreated = true;
}

Also, you have a stray closing curly brace, and it's probably better to check for strict equality (e.g. ===).

Using a linter will catch things like this out of the gate. Here's a tutorial on using ESLint, for example.

James Hibbard
  • 16,490
  • 14
  • 62
  • 74
1

This has nothing to do with scope.

The if test must be between parenthesis: if (condition)

if (daysCreated === false) {
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

This is not valid. You need brackets for your if statement like this:

function createDays() {
 if (daysCreated == false) {
    //do something
  }
  daysCreated = true;
}
Tom el Safadi
  • 6,164
  • 5
  • 49
  • 102
0

You have a typo

function createDays() {
   if (daysCreated === false) {
       //do something
   }
   daysCreated = true;
}

There was a stray {