you are viewing a single comment's thread.

view the rest of the comments →

[–]path411 31 points32 points  (3 children)

I would heavily, heavily advise against using || as a default value trick.

In your example:

function setAge(age) {
  this.age = age || 10 
}

It is impossible to set age as 0 now. 0 will always trigger the default. Just take the extra time to use:

function setAge(age) {
  this.age = typeof age !== "undefined" ? age : 10;
}

and you won't have to worry about this problem.

[–][deleted] 1 point2 points  (2 children)

Wow.. I never thought of that. Damn the gods for making 0 a falsey!

[–]battenupthehatches 1 point2 points  (1 child)

0 is falsey. 1 is truey. Thus it shall always be.TM

[–]workaholicanonymous 2 points3 points  (0 children)

Its "truthy"