-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.

4
  • 1
    This is not valid syntax. The condition in the if needs to be wrapped in brackets. Commented Jun 11, 2017 at 14:56
  • Possible duplicate of javascript if statement syntax (need help) Commented Jun 11, 2017 at 14:58
  • By the way if(!daysCreated)... Commented Jun 11, 2017 at 15:07
  • I would likely have passed the parameter to the function rather than working with a global daysCreated - and returned that "internal" value, keeps the code a bit more modular perhaps. Commented Jun 11, 2017 at 15:08

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

This has nothing to do with scope.

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

if (daysCreated === false) {

Comments

0

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

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

Comments

0

You have a typo

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

There was a stray {

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.