Hacker News new | past | comments | ask | show | jobs | submit login
Ask HN: Changing my mind about JavaScript
54 points by Clyd3Radcliff3 33 days ago | hide | past | favorite | 67 comments
I'm a longtime developer software developer with statically typed languages, in the last few weeks i started working on a couple javascript projects and i must say that i'm changing my mind about javascript, albeit i still think that for bigger project relying on statically typed languages is a better and safer path, i think that JS deserve a chance to be "really" learned, i'm not referring following the dozens of tutorial we find online that are pure crap (using dozens on stuping npm packages or using pre-cocked solutions without really understanding it's full potential).

Do you have any tip for learning js at it's fundamentals? internals, advanced patterns and everything that can relate to understand it in deep?




I have used many languages in my nearly 40 years as a programmer (Scheme, C++, Java, Python, Haskell, OCaml, Rust, etc.) and still enjoy JavaScript: It’s decent as a language and there is so much you can do with it.

Tips:

• If you like static typing, you’ll want to use TypeScript. It’s more work to set up, but it catches many bugs, especially subtle ones where JavaScript’s semantics are not intuitive.

• I learned a lot about JavaScript (and Node.js) by writing shell scripts in Node.js.

My books on JavaScript, TypeScript and Node.js shell scripting are free to read online and target people who already know how to program: https://exploringjs.com


> If you like static typing, you’ll want to use TypeScript. It’s more work to set up, but it catches many bugs, especially subtle ones where JavaScript’s semantics are not intuitive.

It’s worth looking at JSDoc as an alternative to regular TypeScript. No compiler to set up, and you’re restricted in a good way - less likely to get over-engineered types.


I find writing it too hard compared to TS, but in terms of functionality it's pretty good.


How does your book Deep Javascript compared to You Don't Know Javascript? I just finished a Eloquent Javascript and looking for something advanced.


> I learned a lot about JavaScript (and Node.js) by writing shell scripts in Node.js.

Totally! This will give you lot of insights into low-level working of JS. Of course, some Node.js specifics too. Reading from STDIN, parsing strings into structured data etc.


For writing shell scripts in Node, there’s a helper library called zx created by Google.

I find it a lifesaver for the common occasion when I’m tempted to write a bash script but also know it’s going to need some slightly more advanced features (as in parsing a JSON, or just arrays that are not totally bonkers in syntax).


Yes, I was about to propose this. But then again, the author wants to learn the basics without grappling with abstractions. I think zx can be a bit quirky.


For sure there's an element of magic to zx. The use of tagged template literals doesn't look like plain JavaScript even though it's standard.

You need to understand async/wait, promises and tag functions to make sense of this basic zx example:

  await $`ls -la`


I did a lot of coding with C++ and ended up moving to 100% JavaScript for our game creation tool Construct (www.construct.net).

I'm now a huge fan of JavaScript - I think it's a really underrated language. People often still talk about it like it's 2010. The modern language features are amazing, the performance is incredible - including the GC performance - and it's memory safe and no need to worry about allocations. If you want static typing, use TypeScript, which is also very good and is basically a static type layer over JavaScript.

My advice is look at the modern language features which now have wide cross-browser support, and be sure to use them. It can significantly change your coding style for the better. By that I mean things like modules, private class fields, nullish coalescing, optional chaining, async/await, map/set - some of which are fairly old now but are still getting new improvements worth knowing about.


If you have plenty of time, start a side project to build out a HTML canvas library (with no dependencies) ... just kidding! I've been building my canvas library for 11 years and there's still a shedload of JS understanding I've not yet poked.

More seriously, the MDN documentation is really useful[1]. Some of the advanced topics listed on that page may be a good place for an experienced engineer to start?

- Inheritance and the prototype chain - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inhe...

- Memory management - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memo...

- The event loop - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Even...

[1] - https://developer.mozilla.org/en-US/docs/Web/JavaScript


Static types are sanity when the people and years keep increasing. It isn't at all important in small one man projects since most of your defined types are in your head in any case.

There is nothing wrong with JavaScript as a language beyond it's, originally questionable, foundations that have since been iterated upon (much like PHP).

As a massive fan of static typed languages. There is no denying that dynamic typed languages are more fun to develop with in smaller project. It just they often hit a tipping point when they become a horror to deal with. I can still remember my first legacy Python code based where everything was thrice nested dictionaries.


"It isn't at all important in small one man projects since most of your defined types are in your head in any case."

Yes, today. Not in 7 days, let alone 3 months.


And if you are working on multiple projects as a freelancer, the brain capacity it occupies seriously hampers productivity.

We are creating software to make people more productive, but when it comes to our own productivity many of us interestingly shy away from using software (types, in this case) to enhance our own.

I did this mistake for years. Cost me dearly.


I often hear people say this, but in my opinion, static types save a ton of trial and error and help you out even for a 20 LoC script.


And have great discoverability when fully integrated into an IDE.


Type safety is important but 80% of the infra is built on bash :rolleyes:


I would recommend using Typescript and a strict linter if possible. TS is essentially modern Javascript plus static typing (so by learning TS, you also learn JS as a side effect), and strict linting rules will reject outdated Javascript features and nudge you towards the 'good parts'.

With those two things (a static type system layer, and strict linting) JS is actually a quite decent language nowawadays, especially in VSCode (e.g. see: https://code.visualstudio.com/docs/languages/typescript).


I would also recommend Typescript. Anything important you need to know about Javascript, you will end up picking up along the way.

My main complaint: the default compiler settings are pretty loose, presumably in the interests of erring on the side of letting Javascript code through by default. I'd recommend going through the available project settings carefully and switching on more of the checks. eslint is also a good idea I think - and a similar comment applies. (I activated pretty much everything, and dialled it back as I found myself getting annoyed by things that felt like poor value for moneey.)


My biggest gripe with Typescript is how much more difficult (Except perhaps in Bun/Deno) it is to set up compared to vanilla JS.


npm install ts-node

ts-node ./my-typescript.ts

doesn't seem difficult...


Until you have to deal with Jest for testing, or React.


Even easier than that:

    npx tsx ./file.ts


JavaScript is flexible and simple in a good sense of the word.

There not much advanced to learn.

Thanks to single threaded design you basically just write whatever you intend to do and it works.

With a proper mix of oop and fp there is no place for “advanced patterns” and other over-engineering diseases in a lean js world.

The most terrible and ungly js code ive seen are from former .net and java developers.

I suggest you unlearn all the “patterns” and keep it as simple ad possible.


> Do you have any tip for learning js at it's fundamentals?

I would recommend:

- https://eloquentjavascript.net/

- https://javascript.info/


When I needed to learn JS above the 'hack it out' level, Eloquent JavaScript was really great. It really focuses on fundamental constructs and is not tied to any 'framework of the day'.

I see there's a 2024 edition that I should probably order.

Update: Looks like the 2024 version is going to be released in Nov.


I second both resources!


I third both resources!


I have been using JS for more than 20 years, and I do not like it. There are parts of it I find useful, but I avoid using it as much as I can in any project.

The biggest reason for this being that the execution of JS code it down to the browser rendering the page where it’s deployed. As such it’s practically impossible to get any code to run consistently. This is of course only relevant in my day to day work as a web dev.

That aside, JS has some funny behavior. If you wish toninderstand JS better you could read up on how null and undefined works.

You could also read up on how functions work, they are objects actually.


> the execution of JS code it down to the browser rendering the page where it’s deployed. As such it’s practically impossible to get any code to run consistently

Can you elaborate on this part? I would say one of the strengths of JS is that you can get it to run consistently just about anywhere.


Certainly! Now, I’m no expert and may get some things wrong here, if anyone else knows better I’d be happy to be corrected.

Every browser implements a version of ECMAScript (this is a specification declaring what features of the JS language will be available for the JS engine included with the browser).

This means that every browser on the planet, and every version of them, support different JS features.

For example, let and const are commonly used to define variables, but if someone happens to use an old browser version the use of these would cause an error, which in turn may cause all the rest of JS code to stop executing, resulting in (worst case scenario) your website being rendered useless to that particular person.

On top of that different browsers may use different engines. So even if they define the same ES version the code may still execute differently (this was for example a very big issue with Microsoft browsers in the past).

This may seem trivial, but new JS features emerge all the time. Granted, this was a much bigger problem before ~2015 something, but it’s by no means none existent today.

Thus, you cannot be sure the code you write locally will actually function the same everywhere, and you are unlikely to know how often failures even occur since it all happens client side.


I think with the official deprecation of IE this is mostly a thing of the past since all major browsers are “evergreen” and support a pretty recent standard. This certainly was the case only a couple of years ago, and it’s almost always wise to have a bundler (e.g webpack) that can target a fixed standard as output via transpilation/polyfill just in case you are itching to use the experimental features.


There is a functional core in js, and as such it might be an idea to take a look at common OOP design patterns and how they translate to an FP approach.

This is Clojure based, but there is a lot of similarity to js once you get past the syntax: https://mishadoff.com/blog/clojure-design-patterns/


There's JavaScript The Good Parts by Douglas Crockford.

I've read the book and watched a similar series on YouTube.


Second this recommendation. There's been a lot of changes to JS recently, but it's still worth reading The Good Parts because I think it's philosophically a good way to approach JavaScript. It can be a good language if you know which parts to avoid, and which quirks to look out for.

There's also https://github.com/getify/You-Dont-Know-JS as a good follow-up to The Good Parts.


This book was excellent for the time, but is now quite outdated. Stuff like how he proposes modules is completely irrelevant with ES modules, arrow functions make ‘this’ much easier, and we have block scoped bindings


Seconded. I won't recommend it and no one I know has recommended it for a decade. It's hard for someone who doesn't know JS to know which parts has changed and is no longer the way to do things.

https://github.com/getify/You-Dont-Know-JS and

https://eloquentjavascript.net/ are the 2 best source for learning JS.

If you don't have time to read both, just go with https://eloquentjavascript.net/

If one needs to go further, go through http://superherojs.com/ , just ignore links that are dead.


He did a marvellous and very entertaining lecture series on JS:

https://m.youtube.com/watch?v=JxAXlJEmNMg


Been coding for 30+ years. In the last 5 years I have come around to not hating JavaScript (TypeScript really), but let's be honest, it took 15 years of hearing the JS community tell us "sure JavaScript used to be awful, but now it's finally good" for it to actually be true.

I remember clearly the days of callback hell. You didn't change your mind about JavaScript, JavaScript changed it's mind about being a real language.

I will give it credit, I don't know of another programming language that has changed so drastically and so much for the better.


I've been doing JS for 18 years. I don't think it particularly deserves to be learned but I think in general you should know a scripting language and JS is very popular (and can be embedded with v8 isolates fairly easily). If you know python or lua I wouldn't bother. Lua would probably be my first choice.

Scripting languages are faster for prototyping but they are a nightmare for maintenance. TypeScript might look like a good middle ground but the many escape hatches really allow you a single bad coder to wreck havoc in your codebase. You can't trust a codebase you don't know in TypeScript the same way you would trust a Rust codebase.

JS is good for job security because the node.js hype cycle brought a lot of JS in enterprise places - it's been used for massive codebases and now there are plenty of microservices nobody wants to touch or know how to debug. The rise of horrible, enterprisey frameworks (eg. NestJS) helped this. JS could be the COBOL of the next generation, if it weren't for the web, which will likely keep JS alive for a long time.

If you really want to learn JS, focus on the good parts.

Avoid classes and `this`, avoid prototypal inheritance.

Postpone the frontend as much as you can as that changes every 5 minutes.

Start with bun (if you have no dependencies on node, it's not really compatible), so you'll have no issues with imports / require and all the tooling / transpiling (which rightly give JS a bad name)


JavaScript is one of my favorite languages. It has the nearly unique design constraints of having multiple implementations and being unwilling to break backwards compatibility, which gives it a lot of warts that are easy to ridicule. But I think the language on the whole is very flexible, very simple, and very usable.

I would recommend getting your head around closures. It’s possible to statically determine what variables exist in which scope, and so if you can nail down those rules it makes analyzing code much easier.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Clos...

I would recommend a light linter. I see too much new JavaScript code using `var` written by non-JS devs in 2024.

But I don’t think you need TypeScript if you’re first learning the language. Instead, revel in the dynamically-typed flexibility, and learn the error messages JS gives. There are only really 4 or 5 things that will cause JS to error, which can make debugging difficult initially, but once you learn those error messages, it makes debugging comparatively easy.


I have a long history developing stuff in strongly typed languages, C, C++, C#, Go, Rust, Java and Kotlin but have always done the "quick and dirty" in Perl, Ruby or more recently Python or Kotlin.

I still much prefer strongly typed languages for anything that needs to be maintained, and i've always hated JS with a passion.

What finally convinced me that JavaScript might have it's merits was working on an application written in TypeScript. In my opinion (and not everybody elses) TypeScript fixes a lot of "flaws" not only JavaScript but any duck typed language.

I've long used type hints in Python, but they rely on the editor/tooling to find stuff that doesn't match, where TypeScript finds it compile time. Sure, it's all javaScript in the end, but considering that JavaScript can compare Apple and Orages and decide they're all tomatoes, having compile time type checking is a god sent.


I have started with a dynamic language with optional typing (BASIC), back in 1986, went through several static and dynamic typing throughout the years.

While I lean on the static languages front, I am a big fan of dynamic languages as well, like Smalltalk and anything Lisp.

In regards to JavaScript, it isn't the Scheme/SELF we wanted, it is what we got and already quite powerful.

Some advanced stuff, how prototype inheritance works, how the new class model is actually mostly syntax sugar on top of prototype inheritance and not really Java like even if it looks similar, how decorators work (not yet fully there but will come), JIT and GC across the various runtimes, if care about server code how to write native extensions for the various runtimes.


I think the best way to understand/appreciate javascript as a language is to A) separate it from the DOM, and B) take a functional programming focus

Too many times, when people complain about javascript, what they are really complaining about is the hectic nature of web development. You have three trillion dollar companies (Google/Chrome, Microsoft/Edge, Apple/Safari) actively fighting over agreed upon standards for how the dom is rendered and manipulated. Try and learn any language in that environment, and you'll have a very frustrating time

Why functional programming? Javascript was one of the earlier mainstream languages to support passing around functions as first class citizens, and it's often an aspect of the language that gets downplayed. By and large, javascript sits at the sweet spot of allowing you to do cool functional programming concepts, without all the strictness and verbosity of a language like Haskell. Is that a good thing? It depends, obviously. But for the sake of "Change my mind", this is where I bet you'll start looking at it in a different light

I'm not saying it's the best/one-true language. It's just another cool tool to have on your tool belt


I'm surprised that Kyle Simpson's, _You Don't Know JS_ hasn't been mentioned in this thread.

https://github.com/getify/You-Dont-Know-JS

When I was beginning to develop a deeper understanding of JS a few years ago, that book, Kyle's talks, and conference presentations by Chrome's V8 team were very helpful for me. It looks like Kyle has expanded the content considerably since then too.


There is honestly not much to learn, and that might be part of the appeal. At its core, JS is a relatively simple dynamic language with a clean syntax. Despite the clean syntax there are quite a few quirks in how some of the internals work (sorting and pervasive string comparison and casting, equality operator, etc). The most interesting thing about it is probably the programming model of free-form objects and closures: rather than using classes, you could write functions that return free-form records containing functions (and getters) that close over the lexical scope of the function. This gives a kind of dynamic object construction flexibility without the additional boilerplate.

For the base language I recommend Dr. Axel Rauschmayer's books (https://exploringjs.com/) as they're kept very much up to date.

Where things get really interesting is TypeScript, which takes all of this dynamism and manages to model it with a type system that doesn't feel all that constraining. It also ameliorates most of the core language papercuts. I don't really have good recommendations for TypeScript for now, other than browsing typescript issues and pull requests for examples written by Anders Hejlsberg

You have to be very careful with the library and tooling ecosystem, however. There are a billion ways to do things, many of them incompatible with each other, and things break all the time. This includes major, popular libraries and frameworks with good looking documentation websites, so it can be difficult to pick something solid and stable. This is where most of the pain of using JS comes from.


I love writing javascript. It behaves like a lisp which really likes hashtables and runs on some of the best compiler tech anyone has shipped. Typescript is an interesting layer I haven't used enough.

Most stuff written in javascript makes me nauseous. The entire NPM ecosystem is a parody of software development. But you can ignore essentially all of the libraries and still get work done.


I love JavaScript, been programming for more than 16 years now. Built websites, apps, banking portals, chat bots and customer experience software using Js, React, Redux and TypeScript. Also used Java and currently work on Go lang. Even after using every language I still prefer JS to be my everyday work love language. Its better now and improving too.


> Do you have any tip for learning js at it's fundamentals?

Build something you think is fun. Take a static index.html page without any build tooling (just open the file with a browser), link a JS & CSS file to it, and make a cool thing with animations or interactivity. Maybe try to remake some cool interactive thing you saw somewhere


Once you understand the fundamentals, knowing about the newer stuff would be a good step forwards:

https://github.com/tc39/proposals/blob/main/finished-proposa...


I strongly suggest TypeScript, which is (as you probably already know) JavaScript with static typing: in other words exactly what you want.

TypeScript adoption over plain JS is HUGE in the recent years, I wouldn't be surprised if it becomes the native default in browsers some time in the future.


The industry uses TypeScript these days. If you’re inheriting a project, write the new stuff in TS, and gradually migrate. If you’re using the modern syntax, I’d say JS behaves mostly like every other language, with the exception being the tooling landscape is massive


I started my career with JavaScript and Node.js, but over time I've grown to dislike its design flaws and dynamic nature. If JavaScript had Dart's strengths, I might still be using it; instead, I've moved on to languages I find more respectable.


My (unpopular?) take is that JavaScript should suffice.

Once I feel the need for TypeScript, I've done something wrong. For example, built my whole frontend with JavaScript or built my backend in JavaScript.

JavaScript's use case for me is sprinkling it where needed in browser. Anything extra and it feels wrong tool for the job.

Frankly, I believe whole existence of TypeScript is a mistake – we should've never used JavaScript at a scale where static typing becomes necessary.

Having said all that, I'd love to be able to write gradually typed JavaScript (i.e. TypeScript) in the browser. Gradual typing is still greatly beneficial, even in my sprinkles.


How would you not build the whole frontend in JS though? Use another compile-to-JS language?


Render most of the frontend server-side and just use JavaScript for the dynamic bits only, I guess? I personally haven't done it like this since the advent of SPAs, but I'm guessing lots of things are still built like this.


It *should* still be the default.


I urge you to check out https://hypermedia.systems/ (free to read online).

TL;DR: The web platform has evolved (and continues to evolve) so much that I don't see a need to use SPAs for the majority of web applications.


The power of JS is the amount of effort put into its optimization, because of its importance for the web.

JS is no longer as slow as people say, especially when developers are cautious about memory allocations and time complexity.


I don't write JS, but the Wat talk makes me want to stay as far away as possible

https://www.destroyallsoftware.com/talks/wat

C has lots of uglyness and quirks but it has the excuse of being ancient - when people knew less. Zig/Hare are attempts to revisit the space with cleaner designs

Somehow several decades later something even more gross was created. No thanks. And the "well just stick to the good parts" stinks of C++

Thankfully there's Clojurescript if I ever need to touch the browser


I think TypeScript is a legitimately fantastic language and I've used everything from C++ to Haskell.


Cristian Salcescu‘s books on JavaScript are absolutely marvellous and must-reads.


JS is broken and TypeScript is a way of fixing it but at the end of the day, you use Frameworks anyway like Anguular.

If you just want to hack around it, hey just use it as any other scripting language


JS isn’t any more or less broken than every other language under the sun


PHP has a lot of issues in comparision to java and other languages.

JS has also missing features and semantic which got added through typescript.

I don't want to dismiss langauges because of this but saying 'every other language' is not true and not true in an objective sense either.


I mean why not use Typescript and have both?

Projects don't have to become very large at all before static types are better & more productive than dynamic types. Especially for multi-person projects, or ones that last for a while so you forget stuff.


agree with this, at this point all my toy projects, even those of a few lines, are in typescript (with bun.js so I don't have the overhead of configuring node for ts)


Yeah I use Deno for the same reason.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: