37

I have a piece of code that tests for the existence of a variable, using an if statement like the example below. I need to do one thing if the var is set, a different thing if its not. In a certain test case, the var needed to be set to 0, but that is the same is having an unset var in JS, apparently:

var toMatch;
toMatch = 0;
if (!toMatch) {
    document.write("no");
} else {
    document.write(toMatch);
}

// html is "no"

jsFiddle

So my problem is, how do I test for a var if its value is legitimately zero. I should point out, that in my function possible values to be passed are 0-40+.

In the past I've used a work around like setting the initial value to a number high enough that it is not likely to be passed to the function but that seems hackey to me. Is there a better way to do it?

inorganik
  • 24,255
  • 17
  • 90
  • 114
  • also worth noting that you should check how that var is created, if it's a user submitted form then if(toMatch == "0") may work, that was my case when all the accepted answers kept failing – Robert Sinclair Mar 05 '19 at 16:35

4 Answers4

46
var toMatch;
toMatch = 0;
if (toMatch === 0) { // or !== if you're checking for not zero
    document.write("no");
} else {
    document.write(toMatch);
}

toMatch === 0 will check for zero.

toMatch === undefined will check for undefined

the triple equals are strict comparison operators for this sort of scenario. See this blessed question: Difference between == and === in JavaScript

Community
  • 1
  • 1
Simon Sarris
  • 62,212
  • 13
  • 141
  • 171
11

Instead of

if (toMatch)

use

if (toMatch == null)
Sophie Alpert
  • 139,698
  • 36
  • 220
  • 238
  • 1
    -1 because you should always use absolutely equal === when comparing to null/undefined (key words) – Eric Hodonsky Feb 07 '12 at 21:02
  • 12
    No, `x == null` is equivalent to `x === null || x === undefined`. Comparisons to `null` are the only case that you should use `==` (for example, the jQuery style guide endorses this practice). The fact that null and undefined are reserved words is irrelevant. – Sophie Alpert Feb 07 '12 at 21:03
  • I know this, this is just lazy though... and you're allowing possible exceptions, unless this is the specific behavior you're looking for. Good JavaScript practices always say use absolutely equal, but I've found 1 or 2 places in all of my code that it's buggy in IE so I have to use relatively equal. And of course jQuery endorses this, they're the ultimate cross-browser library. Thats why they have such a huge stake in the market share. – Eric Hodonsky Feb 07 '12 at 21:05
  • It's very rare that you actually care about the distinction between `null` and `undefined`. If `toMatch` were a global then I'd suggest a `typeof` check but since it's (apparently) not, there won't be any exception -- arguments to functions and local variables don't throw exceptions when you access them if they're not defined. I've done lots of development in IE; I can assure you there's nothing wrong with this comparison. – Sophie Alpert Feb 07 '12 at 21:09
  • I didn't say wrong, I said lazy... programmers are inherently lazy, thats why we program, we find a problem and try and write code to solve it for us because... jump the actually math, I got a calculator. – Eric Hodonsky Feb 07 '12 at 21:17
2

You can see if a name is undefined with:

if (typeof bad_name === "undefined") {
Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
0

Javascript is a bit funny when it comes to values and boolean checks. I suggest reading about Truthy and Falsey in Javascript.

You should use the identity inequality operator !==:

var toMatch;
toMatch = 0;
if (toMatch !== 0) {
    document.write("no");
} else {
    document.write(toMatch);
}

It is also worth understanding the differences between == and ===.

Community
  • 1
  • 1
Oded
  • 489,969
  • 99
  • 883
  • 1,009