One day I googled “how to become good at javascript” and came across this article How to Become a Great JavaScript Developer. It suggests reading well-written JavaScript code and highly recommends underscore.js among several great libraries. So here we go!


var shallowProperty = function(key) {
  return function(obj) {
    return obj == null ? void 0 : obj[key];

This is a typical example of closure. It takes a key as argument and returns a function which gets the object value using the key we previously assigned. Here is one of its use case:

var getLength = shallowProperty('length');

getLength becomes a function that returns obj['length']. Now let’s take a look at the definition of Closures by MDN:

Closures are functions that refer to independent (free) variables (variables that are used locally, but defined in an enclosing scope). In other words, these functions ‘remember’ the environment in which they were created.

In this case, getLength is a closure that incorporates both the anonymous function, which gets the value by object key, and 'length' key that existed when the closure was created.

void 0 vs undefined

You might wonder what exactly is void 0? If we try the following in the console:

> void 0 === undefined
> true

Hmm…they are exactly the same! So what is the benefit of using void 0 over undefined? It is because void is an operator, which cannot be overridden in JavaScript and always returns undefined. However, since undefined is a global property, in older browsers, it can be changed:

> console.log(undefined)
> undefined
> var undefined = 1
> console.log(undefined)
> 1

Another legitimate reason for using void 0 is that it’s 4 characters less than undefined!