· javascript

Notes on JavaScript: Logical Short-circuit

Following on from my last two posts (1/2), I was reading Eloquent Javascript over the weekend and learnt about a behaviour of logical operators relevant to both posts.

This bit of code from the exercism.io “Hello, world” exercise:

name = name || 'world';

gives the name variable the ability to be either the string 'world' or the variable name. Which one it ends up being depends on whether the logical operator || reads the variable name as true or false.

The || works by converting the value on it’s left to a Boolean, and if it’s true then it will use that value, and if it converts to false it will use the value on it’s right.

> node  // Yep, node has a REPL!! Awesome!
> var name = false;  // This could also be null or '', but not ' '
> name
false
> console.log(name);
false
> console.log(name || 'Anonymous');
Anonymous
> name = 'Son House';
'Son House'
> console.log(name);
Son House
> console.log(name || 'Anonymous');
Son House

If we switch the values on either side of the ||, then the left hand side will always convert to true and this will essentially make using this technique redundant.

> name = 'Son House';
'Son House'
> console.log('Anonymous' || name);
Anonymous

This behaviour gives us a technique similar to the Python optional argument, and we can rewrite our Python the_blues function, from previously, in JavaScript as such:

> var theBlues = function(name) {
... name = name || 'You';
... console.log(name + ' got the Blues.');
... };
> theBlues();
You got the Blues.
> theBlues('Son House');
Son House got the Blues.

And what about &&? Well, it does the same except that if the value on its left converts to false, rather than true, it returns that value, otherwise returning the value on its right.

> var name = false;
> console.log(name && 'Anonymous');
false
> name = 'Son House';
'Son House'
> console.log(name && 'Anonymous');
Anonymous

Eloquent Javscript highlights the fact that ‘the expression to their right is evaluated only when necessary.’ So, for || if the expression on the left is true then, no matter what it is, the expression on the right will not be evaluated. and for && if the expression on the left if false, the expression on the right will not be evaluated. This is called short-circuit evaluation.

As an aside, my solo exhibition in 2011 was at a gallery called ANDOR. Funny, huh?

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket