Javascript || or operator with a undefinded variable
- by bryan sammon
I have been doing some reading lately one article I read was from Opera.
http://dev.opera.com/articles/view/javascript-best-practices/
In that article they write this:
Another common situation in JavaScript
is providing a preset value for a
variable if it is not defined, like
so:
if(v){
var x = v;
} else {
var x = 10;
}
The shortcut notation for this is the
double pipe character:
var x = v || 10;
For some reason I cant get this to work for me. Is it really possible to check to see if v is defined, if not x = 10?
--Thanks.
Bryan