While there are many possible errors made in JavaScript, these are the most common mistakes made in day-to-day use:
Writing embedded or inline JavaScript, rather than linked
We link JavaScript files for much the same reasons we write the majority of our CSS in an external style sheet: doing so separates behaviour from presentation, makes the code earlier to maintain, and allows the same script to be used on multiple pages without replication or redundancy. There are reasons and causes for embedding your JavaScript code directly on a page, but they should be rare.
Not wrapping HTML comments around your embedded code
If you cannot avoid writing embedded JavaScript, you should write it within an HTML comment:
- <script type=”text/javascript”>
- <!—
- alert(“Active JavaScript”);
- -->
- </script>
This achieves two things:
It shields the embedded JavaScript from the W3C Validator, which ignores anything contained within comments; otherwise, there is the possibility that the validator will confuse JavaScript for HTML, and throw errors.
It protects the JavaScript from confusion in older browsers that do not understand JavaScript at all (now a rarity, but still present).
Not delaying the execution of scripts
When you run the code above on a page, the alert window appears before any body content: you must click the “OK” button to get past the alert window. This is a frequent problem with neophyte JavaScript programmers: JavaScript written without a delay means that it has nothing to work on, as no body content has appeared when it executes. In traditional JavaScript, this delay is written as the following:
- window.onload=function(){
- // JavaScript code written here
- }
In JQuery:
- $(document).ready(function() {
- // JavaScript code written here
- })
Changing case in JavaScript
JavaScript is case sensitive. This is not only true of JavaScript code, but also of any variables or constants you create within it. tokenOne is a different variable from TokenOne. Scripts often break because you are trying to test or set the value of a variable that does not exist.
Confusing Concatenation & Addition
Concatenation is the joining of two or more strings. Addition is what you learned in school. In JavaScript both operators use the same + symbol to achieve their ends. It’s important to remember what you are joining together, how and why, and anticipate the result, in order to avoid errors:
In JavaScript:
- alert(10 + 12);
- // result is 22
- alert(10+”2”);
- // result is “102” – note that this is a string, not a numeral
- alert(“There is no “ + “cake”);
- token = 2;
- alert(10 + token)
- // result is 12
- token = “Two”;
- alert (10 + token);
- // result is “10Two”
Confusing single and double quotes
Single or double quotes can be used in JavaScript operations with strings. This works out well until you get them confused, use one to start a string and the other form to end it, or use a single quote in place of an apostrophe character.
- alert('Two’);
- // result is Two
- alert("Two's");
- // result is Two’s
- alert ('Two's');
- // result is an error
Not providing a fallback for an externally sourced script, especially a framework
While Google’s servers are unlikely to fail, it is still possible. Sourcing your JQuery from Google has many advantages, but you should always provide a fallback. My preferred solution is the one popularized by Paul Irish’s HTML5 Boilerpate:
- <script src=//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js>
- </script>
- <script>window.jQuery ||
- document.write('<script src=/assets/scripts/jquery-1.6.2.min.js><\/script>')
- </script>
This provides a fallback option if Google’s servers are unresponsive; obviously, you must have a copy of the script in the assigned location (a copy of JQuery in /assets/scripts, in the case shown above).
Thanks, Tim! It's easy enough to generate by using PHP: the only downside is that doing so means that the CSS has to be dynamically created each time. And as for the tagline... I am in two minds about that. Part of me likes keeping it slightly obscure, as a kind of site Easter egg. (It changes during Halloween, too ;-)
so we don't need the jQuery minimum length anymore:) cool!


