In defense of JavaScript oddities
Tags: javascript, languages, oop
Published: 2019-02-04 07:43:24

---
title:  In defense of JavaScript oddities
published: true
description: A defensive argument on the alleged oddities of JavaScript, one of the most common and misunderstood programming languages
tags: javascript, languages, oop
cover_image: https://cdn-images-1.medium.com/max/1000/1*Gl3_DbzGC1KRtHqVSjjVNg.jpeg
---

My fellow members! Ladies and gentlemen of the court!
My client has too often unfairly been an object of ridicule and public derision. Too many times developers, especially the developers of other languages, [have made fun of him](https://charlieharvey.org.uk/page/javascript_the_weird_parts) and [laughed at him](https://www.destroyallsoftware.com/talks/wat). They insist on considering it, so to speak, [weird](https://www.smashingmagazine.com/2011/05/10-oddities-and-secrets-about-javascript/), [bizarre](https://medium.com/javascript-non-grata/the-top-10-things-wrong-with-javascript-58f440d6b3d8), [geeky](http://www.davidarno.org/2010/05/18/why-javascript-is-a-toy-language/).

Most of the reasons for these attacks on my client are based on alleged oddities such as

```javascript
[] == ![]    // -> true
```

or

```javascript
NaN === NaN             // -> false
Math.max()>Math.min()   // -> false
```

or even

```javascript
[] == ''   // -> true
[] == 0    // -> true
[''] == '' // -> true
[0] == 0   // -> true
[0] == ''  // -> false
[''] == 0  // -> true
```

What are we talking about? What are the oddities?

What’s strange about the fact that, if you submit this request to my client

```javascript
{} + []
```

he answers with `0`? Where’s the oddity?
Perhaps the strangeness is not so much in the answer as in the question. How can you expect, if you ask my client such a shabby question, to get an answer that makes sense?
My client answers `0`, but he might as well answer `42`. I do not see why a different answer could be considered less strange than this one.
Perhaps he’d be better off doing what others languages do in this situation, that is raising an exception, getting angry and probably insulting their interlocutor?
No, sirs. My client has a sense of humour and has no intention of offending anyone.
He realizes the absurdity of the request and responds in tone, with an answer of the same tenor.

The classic objection, gentlemen of the court, is that not only my client is strange, but he is not even consistent, since to the following request

```
[] + {}
```

he does not respond again with `0`, but with `[object Object]`.

Even here, where is the strangeness? Who said that the `+` operation between an array and an object is commutative? No one is surprised that the result of `"foo" + "bar"` is different from the result of `"bar" + "foo"`. But if my client gives a different answer when you change the order of the operands, then he is strange.

Another objection against my client is that it is not [a true OOP language](https://medium.com/@andrea.chiarelli/is-javascript-a-true-oop-language-c87c5b48bdf0) because it has no classes, or rather [the classes it has are not real classes](https://medium.com/@andrea.chiarelli/a-matter-of-class-9c0a0162674b).
First: who said that an OOP language must have classes?
Second: OK, my client has been a little ambiguous in supporting the `new` keyword and the `class` keyword. But he did it in good faith, to feel equal to the other languages that snubbed him.

I can’t hide the fact that my client is a bit different from the other languages, but not so different as to deserve [mockeries and insults](https://whydoesitsuck.com/why-does-javascript-suck/).

The truth is that those who accuse my client of strangeness do not know him and do not want to try to know and understand him.
They are so used to rigid languages and without the slightest sense of humor (I would dare to say, without a soul) that they mistake jokes with strangeness and inconsistencies.
But if only these gentlemen try to know him better, [to enter into intimacy](http://2ality.com/2012/01/object-plus-object.html) with my client, they would discover that [everything has a reason](https://github.com/denysdovhan/wtfjs), that probably the issue is not in the answers that my client gives, but in the questions asked by developers.

*This post was originally published on [my Medium profile](https://medium.com/@andrea.chiarelli/in-defense-of-javascript-oddities-32a80a747cf6)*