DEV Community

loading...
Cover image for Why i hate Javascript

Why i hate Javascript

Mohammad Ali Chraghi
Nelua Lover
Updated on ・2 min read

Alt Text
JavaScript has been one of the most popular programming languages for many years. it is deferentially the most widely used language. But it is nonsense. These are the reasons why I stay away from JavaScript

there's no Static Type

yes, there's no static type and most people think this is good because you can always change a variable type. but no! that's bad for some reasons:

  • decrease Run-time speed
  • decrease Initialization speed
  • programming mistakes
  • unknown data size and then memory leaks

im not really say dynamic type is always bad but this is odd: why javascript have't static typing

illogical comparisons

this one is more famous in JS. Alt Text
not enough? checkout the examples below

"3" - 3  0 
"3" + 3  33
9999999999999999 == 10000000000000000  true
0.1 + 0.2 === 0.3  false // (this website explains https://0.30000000000000004.com )
1.0000000000000001 === 1  true
typeof NaN == 'number'  true
NaN != NaN  true
Enter fullscreen mode Exit fullscreen mode

global variable by default

hmm... how? and why this happen? idk

function my_func() {
    x = 10;
}
my_func();
console.log(x)  10
Enter fullscreen mode Exit fullscreen mode

(i know you can solve it with var or let)
most languages is very sensitive to global variables. even you can't have global variable in rust (you can use static mut but you need to always use it as a unsafe variable).
because global variables can be altered by any part of the code, that make difficult to remember or reason about every possible use also they can't be limited to some parts of the code

sort() function

.sort() function is a method for arrays. but idk why it does't like numeric arrays. see the below example

[8, 19, 24, 1, 5].sort()  [1, 19, 24, 5, 8]
Enter fullscreen mode Exit fullscreen mode

...

i have explained my reasons, but not sure you agree or not. if no explain in comments

Discussion (67)

Collapse
ravavyr profile image
Ravavyr

I've been coding 16 years and never memorized all the stupid rules.
None of these things are "bugs" if you understand the rules of the language.

Here's what I do:
I write code,
I run it,
It errors,
I change it,
I run it,
It works,
I move on to the next task,
and this is how things get built.

Javascript doesn't suck just because it has some quirks. Every single language has quirks. You learn them and make them part of your process.

Or you can complain about them on the internet because it gets you a lot of clicks.

Collapse
stojakovic99 profile image
Nikola Stojaković

Javascript doesn't suck just because it has some quirks. Every single language has quirks.

Not like JavaScript, which has quirks because it was developed in 10 days. And it is a problem because we see more and more software being written in JavaScript even though it's not right tool for the job. There is no problem in using a hammer, but if you're too attached to it, everything will look like a nail to you.

I write code,
I run it,
It errors,
I change it,
I run it,
It works,
I move on to the next task,
and this is how things get built.

That's how I worked too. Then I realized I need to write tests if I want to have a long-term mantainable software.

Collapse
ravavyr profile image
Ravavyr

"It was developed in 10 days"
You are obviously biased about all this, so why bother continuing the discussion.
JavaScript isn't the problem. I do agree a hammer isn't the solution for everything. I come from a LAMP background, but have also accepted that 90% of new websites being created are done with JS so not learning and accepting its quirks is a bad move for the long term.

Tests are useful, but not most of the tests i see people that are like "did this page load? return true"....because that kinda test writing is an utterly useless waste of time.
All i know is, i work on my project until my code runs cross-browser and responsive and the logic is sound. Whether you write tests or not, the end result is what matters.

Hell, for many clients all that matters is if it works on their phone, but that's a discussion for another thread.

Thread Thread
stojakovic99 profile image
Nikola Stojaković • Edited

You are obviously biased about all this, so why bother continuing the discussion.

Everyone is biased. The sentence you quoted is a fact - you can search it on your own.

We can't ignore that JavaScript is flawed. You just can't design a good language in 10 days, no matter how great your skills are or what tools you have at your disposal. I'm not saying we should ditch JS completely, just that we should be more objective. So no, it's not that I don't accept JS and don't move on - I'm working primarily in JS on my job, both on front-end and back-end, and that's why I think I have the right to discuss about these things.

Is JS better today? Yes, it's much better. And with TypeScript, it's possible to remove lot of baggage from your code base.

Tests are useful, but not most of the tests i see people that are like "did this page load? return true"....because that kinda test writing is an utterly useless waste of time.
All i know is, i work on my project until my code runs cross-browser and responsive and the logic is sound. Whether you write tests or not, the end result is what matters.

I don't know where you saw such tests but it's not proper testing. Whenever I'm working on a front-end code base I'm testing rendering of the components, not the whole pages (except if it's a purely static site with no framework at all). There is a reason we check for thing to be rendered - sometimes we may have some error in the component which can cause it to not be rendered on the page and that would seriously impact UX.

Whether you write tests or not, the end result is what matters.

Tests are not written for the sake of writing them. You write tests to ensure that your logic still works. Imagine that you're working on a huge website with bunch of different things happening all over the place. How would you ensure that everything still works as it should? You would always spend minutes and hours testing whole application again and again?

Collapse
fjanoos profile image
firdaus janoos

So basically you are saying you hack away at the code until it works with no particular care for rationality or logic

Thread Thread
stojakovic99 profile image
Nikola Stojaković • Edited

No, I said I write tests for my code. I don't know where you got the idea that writing tests involves no care for rationality or logic when it's quite the opposite case (you need to understand your code very well to be able to write good tests). Writing tests even allows you to spot some possible problems much before the release. You should read more about TDD and testing code in general.

Collapse
blackr1234 profile image
blackr1234 • Edited

Lol you are right but it may take a while to figure out if it has problems.

Collapse
csirolli profile image
Christian Sirolli

The sort function converts each value to strings by default, and then sorts it as a string. You have to create a custom function passed a parameter to sort for this to work as you are expecting. Something like this:

[8, 19, 24, 1, 5].sort((a,b) => a-b) → [1, 5, 8, 19, 24]
Enter fullscreen mode Exit fullscreen mode
Collapse
csirolli profile image
Christian Sirolli

With the double equals, the values are compared after it attempts to covert them to a common type.

With 0 == "0", converting both values to numbers will cause both values to be 0. Similarly, converting both values to strings will cause both values to be "0".

With 0 == [], converting both values to numbers will cause both values to be 0. Similarly, converting both values to arrays will cause both values to [].

With "0" == [], converting both values to strings will cause the left value to be "0" and the right value to be "", so it returns as false. Similarly, converting both values to arrays will cause the left value to be ["0"] and the right value to be [], so it returns as false.

It may be annoying if you don't understand it, but it is logical and be very useful when appreciated.

Collapse
greenroommate profile image
Haris Secic

Not sure I can agree on "understanding". Been using all sorts of languages for more than 10 years now and JS is quite inconsistent. I quite well understand it but dislike the approach. For me it's about taste. Ex. If I always get to convert the values for comparison why use == ever and why use dynamic typing? Using if where you don't care about type is fun but unpredictable as in what to put there? Always use .toString()? Always to number? To array? If I it depends on the use case why not always be strict about type since I know what shod be there?

It's a double edge sword. You win some you loose some

Collapse
csirolli profile image
Christian Sirolli

There are static methods and properties in classes.

Collapse
jwp profile image
John Peters

Relax dude and really, nobody cares that you hate it.

It is the single most dominant language on the planet. So it's better to put in your time until you like it.

It does have some powerful concepts which neither Java or C# have but I'll let you find them.

Collapse
stojakovic99 profile image
Nikola Stojaković • Edited

Where you got the idea that it is the single most dominant language? I'm pretty sure it's C.

As someone who uses JS / TS on daily level, I can assure you that both Java and C# have much more powerful features. Those three are just uncomparable.

Collapse
miketalbot profile image
Mike Talbot

rankings

Thread Thread
stojakovic99 profile image
Nikola Stojaković

I wouldn't use popularity as a metric for the most dominant language but the prevalence. C runs the whole internet infrastructure.

Thread Thread
miketalbot profile image
Mike Talbot

:) Sure I guess C code processes a lot of packets. But the amount of code? Every website? Then every web browser is mostly C++. But again, dominant to me says the language in which most new code is written. If it means something different to you, that's fine.

Thread Thread
stojakovic99 profile image
Nikola Stojaković

Microcontrollers, cars, compilers, web browsers, desktop applications, drivers, kernels, shells, desktop environments... There is huge number of stuff written in C and C++.

On the front-end the goal is to compress your JS as much as you can so it's quite rare to have huge front-end code bases. On back-end PHP is used much more than JS (more than 80% of the websites). Also, you would have to measure private repositories too, for which we have no information.

Collapse
jwp profile image
John Peters

How many web sites are there?

Thread Thread
stojakovic99 profile image
Nikola Stojaković

Read my other comment in the thread. More than 80% of the back-ends are written in PHP. JS on the front-end doesn't take too much (and it would be a disaster if it did because huge number of websites would be slow). Even if you add all websites together I'm pretty sure it wouldn't surpass number of lines written in C across all diferent operating systems, kernels, drivers, embedded code, browsers, desktop environments and bunch of other stuff.

Thread Thread
jwp profile image
John Peters

Python is not 80% where did you get that number. C# has been around for backends for 25 years.a

Thread Thread
stojakovic99 profile image
Nikola Stojaković • Edited

Where did I say Python? I said PHP. See here (it's around 80%): w3techs.com/technologies/details/p...

C# is not used that much on the back-ends overall.

Thread Thread
jwp profile image
John Peters

My bad, but are you talking front-end or backend php.

Thread Thread
stojakovic99 profile image
Nikola Stojaković

PHP can't be used on front-end, it's a server side language.

Collapse
darkwiiplayer profile image
DarkWiiPlayer

Being popular doesn't make JS a good language.

Collapse
jwp profile image
John Peters

True but it's where the jobs live.

Collapse
lexlohr profile image
Alex Lohr

Let's have a look at the examples that make you hate JavaScript, shall we?

9999999999999999 == 10000000000000000  true
0.1 + 0.2 === 0.3  false // (this website explains https://0.30000000000000004.com )
1.0000000000000001 === 1  true
Enter fullscreen mode Exit fullscreen mode

Now every language that handles numeric values as IEEE754 floating point numbers has the exact same issue. The first one can be fixed by using BigInt:

9999999999999999n == 10000000000000000n // → false
Enter fullscreen mode Exit fullscreen mode

The other examples cannot be easily fixed, but will fail in most other languages either.

typeof NaN == 'number'  true
NaN != NaN  true
Enter fullscreen mode Exit fullscreen mode

You have detected an interesting feature of the language to support mathematical functions. NaN is basically an undefined numeric value, thus compares equal to nothing, not even itself. You can detect it by using isNaN. In other language, you'd probably express this with undefined or null, but that would still compare equal to similar undefined solutions, which could lead to mistakes.

[8, 19, 24, 1, 5].sort()  [1, 19, 24, 5, 8]
Enter fullscreen mode Exit fullscreen mode

Having a look at the documentation shows us that Array.prototype.sort will compare the string values of the items for sorting, but you can easily use a lambda function to compare two items:

[8, 19, 24, 1, 5].sort((x, y) => x - y)  [1, 5, 8, 19, 24]
Enter fullscreen mode Exit fullscreen mode

So actually most of your issues either are not actual issues of the language or can be easily solved by learning more about the language instead of expecting it to work exactly like the languages that you already know.

The only valid point you have is that the weak type system can easily lead to errors - and that is already rectified by TypeScript and similar static type checkers.

Collapse
itays123 profile image
Itay Schechner

Well, yeah. You are absolutely right.
However - have you ever used any of the bugs deacribed here in an actual application? I use javascript for a year and a half now, and I had no idea those bugs exist.

Collapse
ravavyr profile image
Ravavyr

Just note that these are not bugs, but just rules the language uses in it logic processing. You don't have to memorize them, but you will eventually run into seemingly "odd" problems that in the end have something to do with these rules, and it will drive you nuts, it's just how it goes.

Collapse
itays123 profile image
Itay Schechner

Of course. It's just more convinient to call those bugs.

Thread Thread
blackr1234 profile image
blackr1234

If they are not bugs, then don't call them bugs.

Thread Thread
ravavyr profile image
Ravavyr

Well, he did say it was "more convenient" lol.

Collapse
blackr1234 profile image
blackr1234 • Edited

The lack of static type makes it easier to code but harder to figure out what's in the object and therefore programs are easier to glitch (theoretically). Code is a bit cleaner when compared to Java with generics.
0.1 + 0.2 not equal 0.3 is a problem that exists in many programming languages incl. Java.
Anyway, every programming language has its best practices which we should all read when we are learning it.

Collapse
thorstenhirsch profile image
Thorsten Hirsch

You know what the other guys did who said Javascript is shit because it lacks static typing? They wrote Typescript!

Collapse
jfbrennan profile image
Jordan Brennan

…instead of putting those efforts into advancing ECMAScript. Thanks again Microsoft for ruining the web.

Collapse
thorstenhirsch profile image
Thorsten Hirsch

You can't advance a dynamically typed language into a statically typed language - it's a different language then!

Thread Thread
moopet profile image
Ben Sinclair

Sure you can!

Just like you can define variables as const or change the scoping rules with a new function definition (=>), you can add something to declare a variable as static typed, or immutable, or whatever you can imagine. That can be bolted on to a language easily enough, then you tell everyone it's "best practice" and leave to simmer for a couple of years.

Collapse
jwp profile image
John Peters

The web is not ruined just your lack of diversity in programming languages.

Thread Thread
jfbrennan profile image
Jordan Brennan

I used Java for about 8 years, used PHP for about 4 years, used to know C# (haven't used it since college), and I'm very slowly picking up Rust at work. I want to learn Python, but just haven't had a strong reason to. I used Perl for one year at my first job and will never ever touch it again. Including JavaScript, that's six and hopefully seven.

Collapse
alichraghi profile image
Mohammad Ali Chraghi Author

i posted about js not ts

Collapse
ryannerd profile image
Ryan Jentzsch • Edited

The ONLY reason JS is popular is lack of choice. Web browsers only support JS. If developers were given a choice of language the majority of web devs (especially seasoned and experienced developers) wouldn't pick the moronic language of JavaScript!

Collapse
jwp profile image
John Peters

This is true. Agreed until now with web assembly.

Collapse
ryannerd profile image
Ryan Jentzsch

Until WebAssembly has DIRECT access to the DOM it will still be JS.

Thread Thread
jwp profile image
John Peters

You are talking about just one interface which we already know. It's a non issue to me and probably most web assembly people

Collapse
riveraser profile image
Sergio Rivera

I have been coding since 2001 professionally when there was a war between javascript (Netscape and the others) vs VBscript (MS Explorer) so I know a lot of Javascript (plain vanilla, jQuery, Backbone, Angular.. Vuejs) and the people who bashes Javascript are usually people that comes from Java the more heavy typed OO out there! And they come to Javascript without understand it! And they want to compare it to Java wtf!!!
When you have more experience all those "negative " about the code goes away and if you don't like it they just go with TypeScript and start creating 20 files (enums, interfaces and again more interfaces, Models, Types and more Types, Services, Controllers, etc) instead of just 5 files for a quick demo in a Web browser 😵😆

Thanks God you did not have to code for Netscape (Javascript) and a quick refactory for VBScript (IExplorer) lol

Collapse
ginsburgnm profile image
Noah Ginsburg

I don't know, I largely come from a python background and good God I hate JavaScript. It's such a fragile language.

Debugging it is a PITA too. I remember one project I worked on (I still hate what I did lol) there was a JS function I wrote that needed to run at load of page and at the end of every ajax call. The load of the page needed to do one extra thing. So I just put a flag in there to state on load right? Nah that didn't work, it'd never fire right on load.

So what ended up in production were two separate, nearly identical functions with a manifesto of a comment apologizing for how it is lol.

Collapse
jwp profile image
John Peters

Yes but it's function construct is a true first class citizen. Does python have that?

Thread Thread
ginsburgnm profile image
Noah Ginsburg

I mean everything in python is a function. I'm not positive what you mean by JavaScript's function construct. But.

In python you can effectively make classes by just embedding function definitions.
You can generate functions programmatically by just modifying the global namespace (it's gross but you can do it, so use only when needed). You can get funky JavaScript like function definitions
Such as:

const sum = new Function('a', 'b', 'return a + b');

by using eval though you shouldn't, because that's gross even in JavaScript and evalis generally a bad idea.

Thread Thread
jwp profile image
John Peters

Nice.

The concept of what Eval does has been around for 50 years or more. E.g. the mainframe language Rexx had an Eval statement. For me, I wouldn't ever say eval is a bad idea as ultimately it's a pseudo run time compiler transforming strings into executable statements. This ability in the right hands is really powerful.

There are rare problems where use of Eval like constructs are true lifesavers.

Collapse
abhishekraj272 profile image
Abhishek Raj
0.1 + 0.2 === 0.3  false
Enter fullscreen mode Exit fullscreen mode

This is same in almost every language.

Collapse
greenroommate profile image
Haris Secic

Some of these are not JS specific but isses with IEEE number things and, OK kind of is, JS defaulting to them. One that caught my eye is NaN != NaN. The issue here is quite a long story but it's in our lazynes, and ease of use from developer side. If NULL != NULL in logic why do we put use it in programming languages. Hint is SQL where you need to do ( at least had to do) IS NULL because no value is not equal to no value it just doesn't exist. So null is just "isn't". Same goes for NaN where not a number is not the same as some other not a number for example NULL is not a number and neither is "textt" so NULL != "textt" is correct. And so is false != "Othertext" .

I really do get it we're all used to some common ways of writing stuff and JS is quite inconsistent which I had argument about recently with another person here on dev.to but some stuff is there for developers because we're used to not because it's logically correct or smart. But again I use class based ( no not OO because classes are not OO) languages so what do I know xD.

Collapse
lewiscowles1986 profile image
Lewis Cowles

The most s**** language. Come on now. You cannot expect to be taken seriously with that title.

I'd recommend revising that.

Collapse
lexlohr profile image
Alex Lohr

No. Programming languages should just do what they're specified to do.

You can't expect a front end language to behave exactly like a system programming language, because it has a completely different set of requirements. But just for example, would you expect a language to use the concept of borrowing for memory management? Rust does a pretty good job doing so, but it is largely unexpected. Let me emphasize again: please don't blame a language for your expectations.

That being said, yes, it might be confusing that Array.prototype.sort is expecting a function to behave well around numbers, but at least it is specified behavior and thus difficult to change. One could attempt to make a proposal to add Array.valueSort(), which is using toValue() instead of toString() for the comparison. Feel free to contact TC39, who is maintaining the official specifications.

Collapse
dirkecker profile image
decker

Could you deleted this Posting please?

Collapse
nagendra0032 profile image
nagendra0032

You don't know JS.
Do read this book..
Javascript do not work like other's language. Everything in javascript runs under Execution context.

Do watch namaste javascript series by Akshay Saini. You will love this. After watching that u will have your answers.

Collapse
yoursunny profile image
Junxiao Shi

Don't like the numbers? Use BigInt and everything will make sense.

Collapse
mahdimomeni profile image
Mahdi Momeni

You do not understand JavaScript, you just learned it for common tasks. So first understand JavaScript then publish the article.

Collapse
alichraghi profile image
Mohammad Ali Chraghi Author

i know and that's why i posted this article

Collapse
magom001 profile image
Arkady Magomedov

A very retarded article. No such thing as a shitty language... You know the ending.

Collapse
spock123 profile image
Lars Rye Jeppesen

2021
Not using Typescript yet, you're doing it wrong

Collapse
heyset profile image
Matheus "Set" Inacio

All those reasons are pretty nonsensical, with the exception of the dynamic typing, which is fair.

There's typescript, though.

Collapse
metammodern profile image
Comment marked as low quality/non-constructive by the community. View Code of Conduct
Bunyamin Shabanov

This is the most shitty post on Dev I've seen so far. You did understand nothing in main principles of the language and create a post called "Why I hate JavaScript". No typing? Daa, cause it's dynamically typed. Stupid comparisons? Go check types in Js, maybe that will help you. Global by default? Well, that's because noone thought that you'll need scoping in a small script which was running on a website back when js was created. That was added later, as well as strict mode. Sort?? Are you fucking kidding me?? Go READ THE FUCKING MANUAL.

Collapse
greenroommate profile image
Haris Secic

Quite inappropriate response full of holes in itself like many StackOverflow resposes. He listed reasons why he hates it. Yes it's dynamically typed and he doesn't like it so what's your comment about? He's listing reasons regardless of why JS has it. "Stupid comparison" is quite there as in he dislikes how it works and so do many. There's no need for checking types it's an observation of how something works and nite about disliking it.

Anyways reported as it's not useful to community. Maybe next time try to understand the point instead of defending the language or just simply hating the author. This is not SO, you don't get points for being too loose on your comments

Collapse
darkwiiplayer profile image
DarkWiiPlayer

Obviously, if this was StackOverflow, the top comment would explain how all these problems aren't a thing in JavaScript and have some code snippets filled with $ without ever even mentioning jQuery at all.

This answer would be the accepted one and have a comment by the user asking the question thanking the poster for the great answer, again, without ever acknowledging that nobody ever even mentioned jQuery.

The next 20 answers would be walls of code using APIs from before we invented the wheel and at the very bottom there'd be an answer from last year explaining how to do the thing in 3 lines of modern JS. Of course, this answer has 0 upvotes and a comment from three months ago pointing out that this code wouldn't work in IE5.

Collapse
metammodern profile image
Bunyamin Shabanov • Edited

I bet you did not report the post for the same reason. That was a hate comment on hate post, the author has no idea how js works, the post is not about "i like this I don't like that", you can see it in the last paragraph where he does not understand why sort doesn't want to sort numbers, despite it IS written in the documentation.
And I'm not the defender of JS, I know it has lots of confusing points, but all of them are explainable including those described in the original post. The author also added nothing to the global knowledge base. Well, maybe if he was one of the creators of a language, or creator of any other language his post would have some weight in community, but he is not. The post is crap, the memes are crap too. You can report as long as you want.

Upd: yes, my response is full of holes(just like the post, lol), I just think it is not worth it to explain fundamentals of js in the comments section or even write a separate answer post.

Thread Thread
greenroommate profile image
Haris Secic

The post is low quality and marked as such. When we do that they are less visible. But, there is one thing, things being explainable have no relation to personal preference. As you said no real value is in there but you can simply ignore stuff insted of being toxic or you could leave a propper comment how post is not well written and details are missing to explain why is something like that and why does the autor hate it. As I said this is not stack overflow to downvote, and scream at people. I've seen numerous of posts like these yet ignored them because what's the point? Some I commented on to point out mistakes if I felt it could be imporved, if not just skip or mark as low quality

Collapse
alichraghi profile image
Mohammad Ali Chraghi Author

i say why js have't static typing