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. :)

 

Here is the code typed out in case you'd like to try it at home.

// Output the factorial of 5

var n = (undefined == undefined) + (undefined == undefined) + (undefined == undefined) + (undefined == undefined) + (undefined == undefined); var output = (undefined == undefined + undefined) + (undefined == undefined);
for (i = (undefined == undefined + undefined) + (undefined == undefined); (i == n + (undefined == undefined)) == (undefined == undefined + undefined); i = i + (undefined == undefined)) {
var mult = (undefined == undefined + undefined) + (undefined == undefined + undefined);
for (j = (undefined == undefined + undefined) + (undefined == undefined + undefined); (j == i) == (undefined == undefined + undefined); j = j + (undefined == undefined)) { mult = mult + output; }
output = mult; } console.log(output);