Can setTimeout ever return 0 as the id?

First: 0 isn't the same as null, (0 == null) would be false in every case';

if you want to test 'a' against something: define 'a' first and later assign the settimeout to 'a'. then check against the type of 'a'. If its 'undefined', the timer hasn't triggered yet


It shouldn't, given this:

handle = window . setTimeout( handler [, timeout [, arguments ] ] )

Let handle be a user-agent-defined integer that is greater than zero that will identify the timeout to be set by this call.


The specifications from Microsoft, Sun and Mozilla just say that it will return an integer. So 0 may be included.

It may be possible (and probable) that some implementations exclude 0 but you shouldn't rely on that. You should go with the !==.

To summarize: Although probably all browser exclude 0 from the IDs returned by setTimeout, you should not write your code with that in mind especially when all your have to do is add an extra =.