The conditional (ternary) operator is the only JavaScript operator that takes three operands and it is frequently used as a shortcut for the if statement. // If isMember (first argument) evaluates to true, return “$2:00” (first expression), // else return “$10.00” (second expression) function getFee(isMember) { return (isMember ? “$2.00” : “$10.00”); } // Another […]

A recipe for responding to a button click with an RxJS Observable. HTML <script src=””></script> <button>Click me</button> JavaScript var button = document.querySelector(‘button’); Rx.Observable.fromEvent(button, ‘click’). subscribe( (value) => console.log(value.clientX) // just for example, log the x position of the cursor ); The observable is created by the operator, fromEvent, which takes an “element” and an event […]

If you want to call yourself a JavaScript bad-ass, then there is at least one little code pattern you should know by heart: the Immediately Invoked Function Expression (IIFE). This pattern defines a function that is executed immediately and it looks like this: (function () { alert(“Hello world, I’m an IIFE!”); }()); This is an […]

As a Java developer, I use a lot of maps – especially instances of HashMap. A map is a dictionary or lookup table that contains key/value pairs. It provides a very convenient way to cache objects for easy retrieval by some identifier such as a string (the key). It’s so convenient, in fact, that I find […]

This is one of those little problems that seem so simple that any bone-head could figure it out. That’s why I felt particularly stupid for struggling with it. Of course, the first thing I did was Google for the answer and since that did not help initially, I thought I should now post the solution. […]