Computing with JS's undefined

Why use numbers or true or false in JavaScript when you can just simply use undefined for ALL values?


From reddit's esolang subredit

Let's break down what the above code is doing, and how it all works. JS is loosely typed, meaning values of one type can be used as another (this is called type coercion). Something that is not true or false will adopt a truth value when used in that context. The first example below is comparing a value with the same value, which we expect to be true. What is odd is that adding undefined to the result will then make it false (undefined is "falsey"):

(undefined == undefined) == true                          // undefined is equal to itself
(undefined == undefined + undefined) == false // undefined is falsey, and we've added it to a truth value


Once we have truth values, we can add them together, to get integers:

(true + true) == 2            // equivalent to this below:
(undefined == undefined) + (undefined == undefined) == 2

// the initial assignment to j:
(false + false) == 0 // equivalent to this below:
(undefined == undefined + undefined) + (undefined == undefined + undefined) == 0

// the initial assignment to output:
(false + true) == 1
(undefined == undefined + undefined) + (undefined == undefined) == 1

Keep going down this road and you'll end up recreating JSFuck. :)

Read More


This entry originally appeared at esoteric.codes/blog/calculating-with-jss-undefined, and may be a summary or abridged version.