Short-circuit evaluation: https://www.google.com/search?q=Short-circuit+evaluation
When you evaluate the || operator, the left-hand operand is 'test', which is a "truthy" value. Therefore, the right-hand operand is not executed at all! This means that the ( myVar = 'Hello there' ) never even happens. -Mike On Sun, Nov 6, 2011 at 11:46 PM, HankyPanky <[email protected]> wrote: > Hey guys, > > According to Javascript's Operator Precedence, we know that grouping > comes before logical OR. So, for the following snippet I expect line 2 > to be evaluated as follows: > > 1) first, the statement within parenthesis, gets evaluated and hence > myVar will be set to 'Hello there' > 2) the logical or is then examined (from left to right, of course). As > the first argument is non-false, the whole statement will be evaluated > to myVar, which has formerly been set to 'Hello there' > > so I expect console.log to print out 'Hello there' but it doesn't, > why? > > /*------------------------------------------*/ > 1. var myVar = 'test'; > 2. console.log(myVar || (myVar = 'Hello there')) > /*------------------------------------------*/ > > > Reference: http://msdn.microsoft.com/en-us/library/z3ks45k7(v=vs.94).aspx > > -- > To view archived discussions from the original JSMentors Mailman list: > http://www.mail-archive.com/[email protected]/ > > To search via a non-Google archive, visit here: > http://www.mail-archive.com/[email protected]/ > > To unsubscribe from this group, send email to > [email protected] > -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/[email protected]/ To search via a non-Google archive, visit here: http://www.mail-archive.com/[email protected]/ To unsubscribe from this group, send email to [email protected]
