Javascript || or operator with a undefinded variable

Posted by bryan sammon on Stack Overflow See other posts from Stack Overflow or by bryan sammon
Published on 2011-01-02T02:49:44Z Indexed on 2011/01/02 2:54 UTC
Read the original article Hit count: 751

Filed under:

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

© Stack Overflow or respective owner

Related posts about JavaScript