Hacker News new | ask | show | jobs
by z3t4 2210 days ago
Strong/weak and dynamic/static. JS is dynamic, but variables will always have a type, which is memory safe - which makes types strong.
2 comments

I’ve heard another definition of strong, which is that the compiler will make more guarantees, and in those cases the terminology was:

    * safe/unsafe
    * strong/weak
    * static/dynamic
Note that while safe/unsafe is basically all-or-nothing, strong/weak and static/dynamic are more of a spectrum, so JavaScript would be safe, weaker, and more dynamic than other languages. Typescript makes JS stronger when authoring code.
Also sound/unsound
I thought weak/strong typing is more about how explicit/implicit type conversion is. In PHP and JS it's almost always implicit and they don't warn you for mixing most types, whereas e.g. Python will complain. That's one thing I don't enjoy with PHP/JS. A lot of certainty goes out of the window with it.
Everyone likes different things.

'1'+1=2 prevents a lot of errors. The languages tries to help you along compared to an harsh error. Maybe you want a harsh error.. halting everything until you declare a new variable with the same type and have to go through a manual conversion. For me let the language handle that. If you are tdding anything unexpected will be caught anyhow if you are worried how things convert.

It's like rust in terms of memory management vs c. It handles it for you..

Implicit string to number conversion is more like C in terms of memory management, in the sense that it's a huge footgun that you will definitely shoot yourself with. I have never seen it prevent errors, only make them worse by hiding them from QA so they can reach production and ruin your weekend.
It's unfortunately in JS that + is used for both string concatenation and number addition. There are however defined rules that anything concatenated with a string becomes a string. And anything used with -/* > < becomes a number. As a beginner in JS I always used -- instead of plus when doing addition. But as I got more experienced I learned what was strings and what was numbers. And if I'm not sure I add an assertion - so that there will be an early error. I also parse all input (user input etc) not even a static strong type system will help you there, you always have to check/parse the input at runtime! JavaScript is strongly typed as in whatever the user inputs it will be converted to a type. JS don't have that many types and usually it becomes a string.