JavaScript is like Othello, it is relatively easy to learn, but takes a long time to master since it can be a tricky language.
Andrew Burgess of net tuts+, recently pointed out 11 poor JavaScript coding practices.
So, if you’re new to the game or a veteran, take a look at these 11 bad practices:
- You’re Using Global Variables
It’s great to have variables that can be accessed from anywhere in your script, but it’s also easy to overwrite the values unintentionally. Burgess goes into more detail on a great solution. - You’re Not Using Semicolons
Sure, your compiler will add it for you, but just like relying on Microsoft Word to fix your misspellings, it’s not foolproof. - You’re Using ==
Using double ‘=’ instead of triple ‘=’. - You’re using Type Wrapper Objects
Leave the typed wrappers up to JavaScript and use the primitive values for numbers, strings, and booleans—arrays, and blank objects. - You’re not Property-Checking when Using For-In
Be sure to clarify your for-in statements to avoid unwanted results. - You’re Using ‘with’ or ‘eval’
Burgess gives two reasons not to use ‘with’: 1 – It slows down the execution. 2 – It’s not always clear what you’re doing. As for ‘eval’, he says: “In a nutshell, you can pass a string of code to the function and it will execute the code.” - You’re Not Using a Radix When Using ‘parseInt’
“When ‘parseInt’ sees a string that starts with a 0, it assumes that it’s an octal number (base 8). That’s why it’s a mistake not to pass a radix; this tells ‘parseInt’ what base the number is in (it always outputs a decimal number).” - You’re Not Using Braces on ‘if’ and ‘while’ statements
Braces are your friend. Use them. - You’re Adding Elements to the DOM Individually
If adding them one by one is a bad idea, then what? Use document fragments. - You’re Not Learning JavaScript
If all you know is jQuery, for instance, you’re doing yourself a disservice. - You’re Following all the Rules
You are only allowed to break the rules, if you know the rules.
This is just a quick list, so you can read deeper about these common mistakes on the net tuts+ website.
Speak your mind...