0

I've watched several Doug Crockford videos about the "right" way to do JavaScript and I've tried to follow his suggestions. Currently in our system we do lot of

if (myvar != null) {
   //do some stuff
}

However, I understand this only works because of type coercion and that myvar is actually undefined until it is initialized.

I have a common block of code in which I've added some new stuff that I only want executed one one page. In that one page's JavaScript I've put a new variable ala:

var doingStuffTheNewWay = true;

In my common block of code I have a test like:

if (doingStuffTheNewWay && someOtherCondition) {
   // new code
} else {
   // old code
}

However, it's blowing up on evaluating doingStuffTheNewWay and not going into my else case. I thought it would evaluate doingStuffTheNewWay as a falsie value, if it didn't know what it was ala Is there a standard function to check for null, undefined, or blank variables in JavaScript?. Any suggestions for how to handle this?

Community
  • 1
  • 1
Eric
  • 2,861
  • 6
  • 28
  • 59
  • 3
    _"it's blowing up on evaluating `doingStuffTheNewWay`"_? With _"Blowing up"_, do you mean it's throwing errors? If so, what errors? If your PC is literally exploding, you might want to remove that line of code. – Cerbrus Jun 23 '14 at 14:25
  • The accepted answer in the question you linked contains the solution (`typeof`). – Pointy Jun 23 '14 at 14:28
  • My computer is literally exploded, which is why it's so important to fix this bug.... – Eric Jun 23 '14 at 14:29
  • @Eric If you actually describe the error that's being thrown, or what happens specifically, you'll get help much faster. – doctororange Jun 23 '14 at 14:30
  • @Eric, joking aside, I was seriously asking for the error code you're getting... – Cerbrus Jun 23 '14 at 14:31

0 Answers0