News for hackers. Stuff that matters.
Re:JavaScript Is Weird by moralestapia on Sunday August 13, 2023 @12:17PM (#37111286)
No, JS is not weird, your code is weird.

true + ("true" - 0) ?

"" && -0 ?

Like, can you tell me a language where it does make sense to "apply AND to an empty string and a (negative?) zero"?

If you write code like this, you have much bigger issues than whatever you choose to write your code in.

Re:JavaScript Is Weird by kerkeslager on Sunday August 13, 2023 @12:38PM (#37111519)
> No, JS is not weird, your code is weird.

My code has bugs occasionally, because I'm a human. Which is why I would like my programming language to return errors when I make obvious mistakes.

> Like, can you tell me a language where it does make sense to "apply AND to an empty string and a (negative?) zero"?

No I cannot, which is why I can't imagine why JavaScript wouldn't return an error for this situation.

Re:JavaScript Is Weird by gloryjulio on Sunday August 13, 2023 @03:41PM (#37113569)
Because js is old and not well designed in the beginning. Its goal includes compatibilities so it's like old windows api that won't change any time soon, or ever. Either use a strict subset or use typescript.
Re:JavaScript Is Weird by kerkeslager on Sunday August 13, 2023 @05:00PM (#37114421)
Sure, except...

How does one enforce a strict subset with an easy setup? Why are people writing non-browser code in this horrible language, often without strict subsets or typescript?

Obviously there are solutions, but the solutions clearly aren't pervasive enough because the problem is still here (and growing, as more JS code is written), so we as an industry still need to keep talking about how problematic and poorly-designed JS is.

Re:JavaScript Is Weird by gloryjulio on Sunday August 13, 2023 @11:51PM (#37117279)
> How does one enforce a strict subset with an easy setup?

The old solution are the good books like the good part. The new solution would be typescript.

It's unlikely the API will get deprecated no matter how you talk about it. It's the windows of internet. Any sane shops would use typescript. U don't even need to write ts, and could just let the compiler do the hinting for you.

Re:JavaScript Is Weird by kerkeslager on Monday August 14, 2023 @11:56AM (#37122552)
> It's unlikely the API will get deprecated no matter how you talk about it.

Deprecation isn't what I want. Equivalent alternatives are what I want.

I still can't do <script type='text/typescript'>. Why?

And look, Typescript is also hampered by some of JavaScripts bad choices. Actor-model concurrency would be a really good way to handle events and parallelism to boot, but we won't get that in any language that compiles to JS.

WASM isn't an alternative, because you can't use WASM without JS.

Re:JavaScript Is Weird by gloryjulio on Tuesday August 15, 2023 @05:49PM (#37139711)
> I still can't do <script type='text/typescript'>. Why?

Because typescript is not javascript. Google already tried with dart, they failed. Typescript is still nice-to-have, it's not replacing javascript. MS is not trying to replace javascript anyway. If your goal is to having javascript replaced, that looks fine but I don't see how it succeed. If you u want to work on TS codebase in backend, there is deno

Re:JavaScript Is Weird by mdwalters on Tuesday August 15, 2023 @06:15PM (#37139979)
>> I still can't do <script type='text/typescript'>. Why?

> Because typescript is not javascript. [...] Typescript is still nice-to-have, it's not replacing javascript.

+1. TypeScript compiles to JavaScript, so it can't replace something it compiles to. Besides, text/typescript isn't even a valid MIME type. The text type is meant for plain-text documents (.txt, .md, etc.), so TypeScript would have been in the application type, following JavaScript (application/javascript). Getting TypeScript in a web browser would be too much work. For starters, they would need to take the TypeScript compiler, slap it into a web browser, where it would compile the TypeScript code, pass the result onto the JavaScript compiler, which would finally pass the result to the browser. Whats wrong with this? The code has to undergo two compilation layers, and, most importantly, the TypeScript compiler in the web browser can be out-of-date with mainline TypeScript releases. Your best bet right now is using a web framework with TypeScript support.

Re:JavaScript Is Weird by hn_throwaway_99 on Sunday August 13, 2023 @12:34PM (#37111480)
That wasn't the point of this exercise - nobody is saying that these are actual code examples you'd see in real life.

The point is that in lots of languages these statements would simply result in compile time errors. In JS, though, the type coercion rules are so loose (and in many cases extremely non-obvious) that you can see this head-scratching behavior.

It's the same underlying behavior that leads to very cool/very insane stuff like JSFuck: https://jsfuck.com/

Re:JavaScript Is Weird by tylercrompton on Sunday August 13, 2023 @12:28PM (#37111417)
I think that it can be both. Yes, weird code will produce weird results but in my opinion, the language should at least help one find the weird code.
Re:JavaScript Is Weird by leeoniya on Sunday August 13, 2023 @12:56PM (#37111703)
TypeScript is great for this, among other things.

it actually aids in writing faster (more JIT-friendly) code by alerting you to these implicit type mutations/coercions.

Re:JavaScript Is Weird by ruined on Sunday August 13, 2023 @12:31PM (#37111443)
js being a dynamic language on the web, it is much more common to unintentionally perform such an input than a non-js developer might expect. typescript helps, but i have fired some of these footguns even with typescript.
Re:JavaScript Is Weird by azangru on Sunday August 13, 2023 @12:18PM (#37111303)
Yes, I was about to suggest that instead of "JS Is Weird", the site would be more appropriately called "My inputs are weird".
Re:JavaScript Is Weird by chefandy on Sunday August 13, 2023 @12:52PM (#37111655)
Indeed, nobody would write this code to get the result shown— weird code is usually an accident, and errors informing you about accidents are way more useful than delivering some weird counterintuitive result just to avoid runtime errors.
Re:JavaScript Is Weird by kdmccormick on Sunday August 13, 2023 @01:02PM (#37111775)
Sure, I'll try. Imagine we had two functions already defined:

  get_current_username: Returns a nonempty string iff user is logged in.

  get_balance: Returns a user's "balance" of some sort of credit or currency. Negative balances are possible.
In Python, where "falsiness" is very clearly defined by the language spec, it would be idomatic to write:

  should_render_balance = bool(get_current_username() and get_balance())
Re:JavaScript Is Weird by positron6000 on Sunday August 13, 2023 @02:34PM (#37112735)
so you get the exact same answer?

in js, Boolean('' && 0) will give you the same answer as bool('' and 0) in python.

if i code golf in python (like the article does in js), i'll also get weird-looking code and "unexpected" outputs.

Re:JavaScript Is Weird by kdmccormick on Sunday August 13, 2023 @03:23PM (#37113331)
Sure, I was just responding to the question:

> Like, can you tell me a language where it does make sense to "apply AND to an empty string and a (negative?) zero"?

People do stuff like that in Python all the time. Personally, I don't do it in JS, though, because I find the conversion-to-bool rules much less predictable. In this case, though, I guess JS would behave the same.

Re:JavaScript Is Weird by ericmcer on Sunday August 13, 2023 @01:23PM (#37111988)
the top one is... kinda crazy, the second one is very-plausible: `if (name && age) {saveUser()}`.
Re:JavaScript Is Weird by gaganyaan on Sunday August 13, 2023 @07:46PM (#37115788)
No, JS is weird. You wouldn't write code like that exactly, you'd write "foo + (bar - baz)". Then a few refactors later, you end up with slightly different types for Reasons, then prod starts blowing up and you have the fun of tracking down why.

This weak typing is unique to a few languages like JS and PHP. Python is just as dynamic, but will at least throw an error if you try to accidentally add a string and a number. Static languages catch this even earlier.