Admittedly I've been guilty of some of these in the past, for your amusement it's the ones in bold:
- Not knowing incorrect (and the very few correct) uses of
eval
eval("obj."+prop);
- Using
with
statements
- Using
parseInt(str, base)
without specifying the base
argument.
- Using
this
in timer/callback functions.
- Using eval-like expressions in timers
setTimeout("someFunc(myScopedVarWhoops)");
- Thinking jQuery is the name of the language you're coding
- Performing simple JavaScript tasks using a framework --
$(1).plus(1)
anyone? ;-)
- Using
continue
without incrementing or adjusting the conditional variable.
- Flooding the global namespace with variables
- Forgetting
var
in or before for
statements. for (i=0;i<10;i++)
- Using an obfuscator and just letting it run wild on your code
- Not really a pitfall, but pointless -
return condition ? true : false;
instead of return condition;
- Not commenting your code, applies to all languages really.
- Using
try...catch...finally
statements to catch errors instead of using if
statements to check variables.
- Foolishly attempting to stop "view source" by blocking right mouse clicks on your pages (I was young *sobs*!)
- Using
{ 0: "Foo", 1:"Bar", 2:"Foobar" }
instead of [ "Foo", "Bar", "Foobar" ]
- Using
parseInt()
on user input
parseInt("1,000") // -> 1, wrong!
+"1,000" // -> NaN, correct!
Some already mentioned:
- Not using strict equality (
===
) operators whenever possible
- Setting event handlers to the return value of a function instead of a reference to said function
- Not
;
terminating statements properly
- Using
for...in
loops on arrays
Might think of some more after I've slept :-)