Ask HN: Changing my mind about JavaScript
54 points| Clyd3Radcliff3 | 1 year ago
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?
54 points| Clyd3Radcliff3 | 1 year ago
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?
rauschma|1 year ago
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
tobr|1 year ago
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.
iamcreasy|1 year ago
scary-size|1 year ago
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.
AshleysBrain|1 year ago
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.
rikroots|1 year ago
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
aswerty|1 year ago
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.
ssijak|1 year ago
Yes, today. Not in 7 days, let alone 3 months.
serial_dev|1 year ago
papichulo2023|1 year ago
flohofwoe|1 year ago
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).
tom_|1 year ago
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.)
timw4mail|1 year ago
aristofun|1 year ago
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.
rom1v|1 year ago
I would recommend:
- https://eloquentjavascript.net/
- https://javascript.info/
mooreds|1 year ago
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.
simulo|1 year ago
bubblebeard|1 year ago
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.
cronin101|1 year ago
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.
emil0r|1 year ago
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/
mo_42|1 year ago
I've read the book and watched a similar series on YouTube.
InsideOutSanta|1 year ago
There's also https://github.com/getify/You-Dont-Know-JS as a good follow-up to The Good Parts.
jacobp100|1 year ago
d13|1 year ago
https://m.youtube.com/watch?v=JxAXlJEmNMg
ryandvm|1 year ago
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.
unknown|1 year ago
[deleted]
jokethrowaway|1 year ago
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)
MatthiasPortzel|1 year ago
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.
8fingerlouie|1 year ago
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.
pjmlp|1 year ago
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.
JoeCortopassi|1 year ago
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
dabber|1 year ago
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.
spion|1 year ago
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.
JonChesterfield|1 year ago
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.
chkry|1 year ago
ativzzz|1 year ago
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
vhcr|1 year ago
https://github.com/tc39/proposals/blob/main/finished-proposa...
can16358p|1 year ago
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.
jacobp100|1 year ago
izolate|1 year ago
727564797069706|1 year ago
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.
gushogg-blake|1 year ago
ivanjermakov|1 year ago
JS is no longer as slow as people say, especially when developers are cautious about memory allocations and time complexity.
contrarian1234|1 year ago
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
chpatrick|1 year ago
d13|1 year ago
Gettingolderev|1 year ago
If you just want to hack around it, hey just use it as any other scripting language
jacobp100|1 year ago
IshKebab|1 year ago
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.
yulaow|1 year ago
saad_123|1 year ago
[deleted]
draw_down|1 year ago
[deleted]
raidendeloof0|1 year ago
[deleted]