Why we love to hate Javascript
Tags: javascript
Published: 2019-10-19 10:47:24

Everyone loves Javascript, and everyone hates Javascript; some people love PHP, some people hate php but very few people hate Python. This is not a JS vs PHP vs Python comparison, no.
A quick Google search shows us that:
JS hated has 20,400,000 hits
PHP hated has 16,900,000 hits
Python hated gets only 2,150,000 hits.

Why is JS hated so much yet used so much. New JS frameworks are getting spawned at a rate of 300 per second (maybe a bit exaggerated). JS is quite simple to pick up and get going, this is why I have met a lot of newbie developers who learn JS from the get go, never invest in learning anything else and just use JS for everything.

Each and every language has its strengths and weaknesses. Javascript has its own special place in hell. One of the major strengths of JS also go back to being its major weakness. JS can be used for both front-end and backend web development. This is leading to a lot of designers trying to become developers. Which leads us to some very broken websites.

Here are some of the reasons why JS is a bane for either front end design or backend development:


The silent fail
JS has a nicely bad habit of failing silently at runtime because of syntactic errors. Syntax errors occur when you try to compile a program in traditional languages but they occur at interpret time in JavaScript.
Here is an example of a syntactic error  caused by a missing a closing parenthesis:

<script type = "text/javascript">
   <!--
      window.print(;
   //-->
</script>

When a syntax error occurs in JS, only the code contained within the same thread as the syntax error is affected and the rest of the code in other threads gets executed assuming nothing in them depends on the code containing the error.

JS is so bad that you must use a Linter for each and every project you work on. In comparison to the other bajillion other languages that exist, I have yet to use any other language that has this strict requirement.


Weak-Typing and Aggressive Coercion

Lets have some fun with code in JS:

    //Adding a String and a number
    var a = "20"
    var b = 40
    print(a + b)
    output: 2040 // Wait, what?
    //Adding an array
    [] + [] → "" // An array plus an array = a String? How?

    //Whatever the heck this is
    12 == [12] → true

None of the above code examples have any logic to them at all. You feed JS illogical arguments, you get a valid response, you give JS logical input, you get back weird nonsense.


Global Hell
function monthsFunction() {
    var Jan = 'This is a local variable';
    /*
    /awesome code here
    */
    Jam = 'This typo creates a global variable’;
  }

Have fun trying to debug that tinny error in a large project.
Here is another more annoying problem with global and local variables:

    function getTotal() {
    var total = 0;
        for(n = 0; n < 10; n++) {
            total += n;
          }
        return total;
      }

May the force be with you as you try to use the variable “n” anywhere else in your code because you did not declare it as a const, let or var. It is now a global variable.


If not JS, what then?
For web development, you need Javascript, you probably cannot do without it. But then try to go with better options that transpile to JS that the browser will accept. You can go with Amber, Brython, ClojureScript and Haxe among others. These are all very good for the frontend and will lead to a lot less of hair pulling on your part.

On the backend, there is no denying that JS is fun for toy projects and all but for serious business you need to bring out the big guns. You can go with the much hated PHP, Java, Python, C# and you can even go with Go.

You really do not want to jump into the WTFs per minute mess that is JS frameworks. To date we have 60 versions of AngularJS,
Backbone, Ember, Knockout, Mercury, Polymer, Mithril and React. The list goes on and on.


Last words
JS is really becoming a necessary evil in the world what with the weight of giants like Facebook behind it. There is a lot of money to be made in the JS industry.
On the frontend side of things, JS is the go-to guy for a lot of projects and has been for a while. It is proving that it is here to last.
The problem is that all these frameworks that spawn also have a lifespan shorter than Ultron’s Age.