Awful parts


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

  1. Global variables - (window.fname = 'Foo'; fname = 'Foo'; var fname = 'Foo';), declare variables using 'var' inside functions
  2. Scope only functions create local scope. Declare variable at the top of the functions.
  3. Semicolon insertion.
  4. Reserved words. var case = 23; SyntaxError: missing variable name
  5. Unicode: JavaScript characters are 16 bit (either UCS-2 or UTF-16. Most of them use UTF-16)
  6. typeof returning 'object' for null and array as well. typeof /a/ can be either 'object' or 'function' depending on browser.
  7. parseInt (leading 0 might make the number base-8 in some browsers. User radix to ensure base)
  8. + can (numeric) add or concatenate strings. It works as 'add' only if both operands are numbers.
  9. Floating point - is not perfect representation. Use integers.
  10. NaN - NaN === NaN // false; NaN !== NaN // true; isNaN()
  11. Phony Arrays
  12. Falsy values
  13. hasOwnProperty is not truely reliable as it is a method that can be replaced.
  14. Objects are never truly empty like a hash in Perl or a dictionary in Python.

examples/js/semicolon.html
<script>
function foo() {
    return
    {
        status: true
    };
}

function bar() {
    return {
        status: true
    };
}

var f = foo();
console.log(f); // undefined
var b = bar();
console.log(b); // Object { status: true }
</script>

examples/js/reserved_words.html
<script>
var o1 = { name: 'Foo Bar' };
var o2 = { case: 'FB'}
console.log(o1);
console.log(o2);
console.log(o1.name);
console.log(o2.case);
// var case = 23;  // SyntaxError: missing variable name
</script>

examples/js/is_object.html
<script>

function is_object(thing) {
    if (thing && typeof thing === 'object' && thing.constructor !== Array) {
        return true; // thing is an object or an array!
    } else {
        return false;
    }
}

console.log(is_object(null));  // false
console.log(is_object([]));    // false
console.log(is_object({}));    // true

</script>

examples/js/is_array.html
<script>

function is_array(thing) {
    if (thing && typeof thing === 'object' && thing.constructor === Array) {
        return true; // thing is an object or an array!
    } else {
        return false;
    }
}

console.log(is_array(null));  // false
console.log(is_array([]));    // true
console.log(is_array({}));    // false

</script>

examples/js/parseint.html
<script>
console.log(parseInt("16"));             // 16
console.log(parseInt("16 little men"));  // 16  (no indication of imperfect parse)

console.log(parseInt("011"));  // 11
console.log(parseInt("08"));   // 8
console.log(parseInt("09"));   // 9

console.log(parseInt("011", 8));   // 9
console.log(parseInt("09", 10));   // 9
</script>