tags:

views:

23

answers:

1

I am trying to use the || operator as the "default" operator as described by Crockford at (http://www.crockford.com/javascript/survey.html):

The || operator is commonly called logical or. It can also be called default. If the first operand is false, null, undefined, "" (the empty string), or the number 0, then it returns the second operand. Otherwise, it returns the first operand. This provides a convenient way to specify default values:

value = v || 10; /* Use the value of v, but if v
doesn't have a value, use 10 instead. */

When I type that into the firebug javascript console it reports an error: ReferenceError: v is not defined, and does not go on to set value to 10.

Is that expected behavior?

+2  A: 

Yes. If you haven't told javascript that "v" is a variable, it will complain. You can do one of two things to correct this:

var v;
value = v || 10;

or

value = window.v || 10;
John Fisher
Thanks. second option looks like what I'm trying to do.
JJ Blumenfeld