Friday, November 27, 2015

double pipe symbol operator in JavaScript

Not unlike the null coalescing operator in C# this gives a failover for an assignment should the thing being assigned be null. For example, while this code will throw up a 4 in an alert it would give a 5 if the contents of the "four" variable were falsey instead of 4...

var four = 4;
var five = 5;
var whatever = four || five;
alert(whatever);

 
 

It's like the wacky double ampersand operator in JavaScript only much less wacky. This thing actually makes sense when you read the code.

No comments:

Post a Comment