It works by using a Destructuring assignment on the array [y, x]. Destructuring is used to extract values from an array or an object. You are able to do this for example:

var [month, year] = [5, 1990]

The example above creates two local variables called “month” and “year” with the respective values 5 and 1990. In the swap example we are omitting the “var” keyword and end up with two simple assignment calls (most ES6 transpilers end up creating the following code):

NeedThis is a website where you can create lists. Those lists are saved server-side and you can share the link to your list with your friends. Entries have three attributes: Name, Link and Status (Not released, Available, Downloading and Completed).

I’ve written NeedThis in Node.js. The lists are stored in a NoSQL database and on the frontend runs Backbone.js MVC. There’s really not much code behind it, but I couldn’t find a similar service on the internet that fits my needs.

The syntax is very similar to Backbone, but of course without any dependencies. This code works best in a modern NodeJS environment where you do not need to support old JavaScript implementations. Bitbucket repository.

Beautiful right? This messy line of brackets, exclamation points and plus signs evaluates to the string “2014”. You can try it by pasting it into your browsers console or using Node.js.

Let’s have a closer look at the first part of it:

[!+[]+!+[]]

This produces an array with one entry: 2. If we break it down even further, it starts to make sense:

+!+[]

This code consists of four elements (differently colored). On the right we got a simple, empty array. The plus sign in front of it automatically casts it into a number, because that’s how loosely typed languages work. We can prove it by running this:

Number(new Array())

Alright, so casting an array into a number results in 0. The next element we need to look into is the exclamation point. The “!” operator inverts a boolean. But our 0 from above is not a boolean. No problem, JavaScript just casts it into one like we would expect it to do in “normal” code. We are now left with this:

!+[]

All we have to do now is simply cast true back into a number by using the plus operator:

+!+[]

Now if we chain two of those together, we get our first value of “2014”; “2”!

+!+[]+!+[]

And that’s the whole magic. The rest is pretty much the same, I won’t explain it all here. There is a site called JSFuck.com which can produce this kind of code. Quite interesting!

LocalStorage is really useful. It can save variables (just like cookies) in a users browser. But there is one problem which many people seem to encounter: Storing objects does not work.

localStorage.setItem('test', { someKey: 123 });

This line of code saves our object as a string. If we restore the stored item, we will get this:

localStorage.getItem('test');
// [object Object]

This is because JavaScript calls the .toString() method on our object, which simply outputs [object Object]. If we want to store and restore objects, we need to encode them into a JSON string. The following code implements my most used solution for storing objects: