DEV Community

Discussion on: My love-hate relationship with JavaScript

hanpari profile image
Pavel Morava • Edited on

Nice article. Actually, types do not change in runtime,it would be pretty difficult to achieve I believe. The variables can be assigned to different types, which is actually possible even in static typed languages. Just think Object in Java, in C# they even implemented type Dynamic to make additional casting unnecessary.

This is valid Java code

public class HelloWorld{
     public static void main(String []args){
        class Dog{}
        class Cat{}
        Object cat;
        cat = new Cat();
        cat = new Dog();

Problem with Javascript or Typescript is their weak type system. Declaring class in modern Javascript doesn't create type, it is still object type, so keyword typeof is almost useless in this regard.

See Python

class Dog:

dog = Dog()
print(type(dog) is Dog)
# >> True

Versus Javascript

class Dog {}
let dog = new Dog();
console.log(typeof dog === 'object');
// >> true

Compared with dynamic, yet strong typed language like Python, Javascript is completely different beast; even Typescript, although typed statically, cannot resolve types in runtime properly, which is understandable, given it is transpiled to Javascript.

Furthermore, as explained in other comment, Javascript was designed in pretty short time as a simple scripting tool. Kudos to its author because I believe he made a great job, given the time he had.

Still, you are wrong if you believe that Javascript was designed as imperative language. In fact, his author wanted to create something based on Lisp, but because popularity of C, he used C-like syntax. This decision probably led to many misconceptions.

Javascript was built on mix of functional, imperative and prototype programming paradigm. Unfortunately, with emerging OOP they tried to fit Javascript into OOP box, which resulted in frustration of many.

Basically, Javascript is Lisp pretending it is Java. Thus, the name.

konstantinklima profile image
Konstantin Klima

Basically, Javascript is Lisp pretending it is Java.

This is the most beautiful oneliner about JS I have ever read. <3

hanpari profile image
Pavel Morava

Thank you. I don't claim it is absolutely true, though 😊

deepu105 profile image
Deepu K Sasidharan Author

Yup, I'm aware of the Lisp legacy and may be I should rephrase what I said about starting as imperative, what I meant was it was used as an imperative language at first

hanpari profile image
Pavel Morava

It is no big deal, really. Just wanted to add few points to your excellent article. :) I believe the type part of my comment bears more substantial importance.