Bad Parts


Based on Appendix B of "JavaScript: The Good Parts"

  1. == and != are bad. Always use === and !==
  2. with statement - avoid it
  3. eval - can be dangerous. Don't use it.
  4. continue - Douglas Crockford recommends to avoid it as you can write code without it.
  5. switch - Always break. Don't let it fall through.
  6. Block-less statement - always use {} after 'if'.
  7. ++ -- - Douglas Crockford recommends to avoid them. I think there are places where they can be used without problems.
  8. Bitwise operators - usually there is no need for them in JavaScript.
  9. Function statement vs Function expressions
  10. Typed Wrappers - avoid new Boolean, new Number, new String, new Array, new Object
  11. new - name constructor functions with leading capital letter, or even better: avoid using 'new'.
  12. void - avoid it

examples/js/function_statement.js
function add(x, y) {
    ...
}

examples/js/function_expression.js
add = function (x, y) {
    ...
};

A good way to enforce that everything is private is to wrap the whole code in an anonymous function expression, and execute it upon parsing it.


examples/js/global_function_expression.js
(function() {
    ...
    // No new global variables
})();

The official ECMAScript grammar states, that if the first word is 'function' then it is part of a function statement, but this is a function expression. That why we add a pair of () around the function.