profile image linking to the home page
PostsJavaScript is awesome but also weird

JavaScript is awesome but also weird

This is an old post from 2020. I'm keeping it here for archival purposes.

JavaScript is awesome. It's pretty popular, so many people agree. But JavaScript can also be pretty weird. Like coming up with a good comparison. It seemed like a good idea at first, but as soon as you write it, it gets weirder and weirder. Eventually you remove it again.

So lets quickly move to some JavaScript weirdness.

Like this: 0.1 + 0.2 = Test live

What happens if you print this out? You get this: 0.30000000000000004

Well that is a floating point issue, where precision is limited. This can happen in other programming languages as well.

Try some of these too:

As you can see, JavaScript tries to convert values to one for that specific context. Because the 'truthy' value of true is 1, you can actually do calculations.

But what about this:

![] + []; = Test live

It returns false, but as a string! That's because JavaScripts detects an addition operation between these two arrays and converts both values to their primitive values. Which is done with the .toString() method. Because arrays are truthy, the first value is false. Then this is converted to a string (try false.toString()) = "false" \ Like this:

1. ![] + []
2. false + []
3. "false" + ""

Now this +[] =

(Test live)

converts an array into a number, which is 0.

Now with this knowledge and because we can use a string similar to an array (e.g. 'false'[0] = 'f'), we can create something beautiful like this: 😎

let hello =
	([] + ![])[+[]] +
	([] + !![])[+!![]] +
	([![]] + [][[]])[+!+[] + [+[]]] +
	([!![]] + [][[]])[+!+[] + [+!![]]] +
	([][[]] + [])[!![] + ![]] +
	([!![]] + !+[] + [][[]])[+!+[] + [+[]]];

Go ahead and try it yourself in your browser developer console! Or click the link below.

console.log(hello) => Test live

Someone made a guide on how to program just with these 6 characters:

If you want to find out more about some JavaScript "weirdness", this is a pretty good collection: