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?

1 Answer 1

3

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. second option looks like what I'm trying to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.