Possible Duplicate:
In Javascript, what does it mean when there is a logical operator in a variable declaration?
Just a quick question. When I declare a variable like this:
var ballctx = ctx['ballctx'] || createCanvas('game-screen', 'ballctx');
Does it try the left first or the right first? I want it to create a new canvas if ctx of ballctx does not exist. If it does, it use that instead.
The left first.
||uses what is called short-circuit evaluation, also known as minimal evaluation, meaning it will only evaluate the right side of the expression, if the left side is false.From the ECMAScript Language Specification:
Thus, in your expression:
If
lvalevaluates totrue,rvalwon’t be evaluated. In other words, you’ll only create a new canvas ifctx['ballctx']evaluates to false, so your code is correct.