Arguments [Link, Link]

function f(x, y) {  
    console.log(x, y);
    return toArray(arguments);
> f('a', 'b', 'c')
> a b
> [ 'a', 'b', 'c' ]

The IIFE Pattern [Link]

(function () {  // open IIFE
    var tmp = ...;  // not a global variable
}());  // close IIFE


Converting to Number [Link]

Prefer Number() instead of +value and parseFloat().

Converting to String [Link]

Prefer String() instead of value.toString().

The Array Constructor [Link]

There are two ways to use the constructor Array: you can create an empty array with a given length or an array whose elements are the given values. For this constructor, new is optional: invoking it as a normal function (without new) does the same as invoking it as a constructor.

Iteration (Array) [Link]

Examination Methods 有 forEach(), every()some(), 其中 forEach() 不支援 break

Transformation Methods 有 map()filter()

Reduction Methods 有 reduce()reduceRight()

Best practice: don’t use for-in for arrays [Link]

Use for or forEach.

Variables Are Function-Scoped [Link]

回想當年我還是 Junior 時,被一個 System Architect review 我的 code 時,他不明白為何我這樣寫不會有 scoping 問題:

function foo(){  
  if (<condition>) {
    var x = 0;
  } else {
    var x = 1;
  return x;

當時我只知沒問題但不知如何回答。現在我會了,因為 JavaScript Variables Are Function-Scoped 哦。(當然現在回頭看,這樣寫的確很怪 ^_^)

Best Practice: Avoid Creating Global Variables [Link]


Named function expressions [Link]

Named function expressions allow a function expression to refer to itself, which is useful for self-recursion:

var fac = function me(n) {  
    if (n > 0) {
        return n * me(n-1);
    } else {
        return 1;
console.log(fac(3)); // 6  

Don't throw string [Link]

// Don't do this
if (somethingBadHappened) {  
    throw 'Something bad happened';
// Do this
if (somethingBadHappened) {  
    throw new Error('Something bad happened');

Don’t Be Clever [Link]

Can't agree more.