Wednesday, March 25, 2009

Improving my JavaScript

I've been listening to Douglas Crockford's The JavaScript Programming Language at the YUI Theater and have learned a number of interesting things that I wanted to note before I forget them.

JavaScript Values: Numbers (64-bit floating point), Strings, Booleans, Objects, null, undefined

False values: false, null, undefined, "" (empty string), 0, NaN
True values: everything else, e.g. "0", "false"

== and != do coercion before comparing (e.g. different types can be considered the same)

=== and !== do not do coercion and the type as well as value must be equal

&& returns the second operand if the first is true otherwise it returns the first operand, e.g.
if(a) { return a.member; } else { return a; }
can be written as
return a && a.member;

|| returns the first operand if it is true otherwise the second as such it can be used to fill in default values, e.g.
var last = input || nr_items;

labelled breaks, a for() loop can be labelled and then a break statement can refer to that label.

No comments:

Post a Comment