4 ms·
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
by moralestapia 3y ago
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.
- azangru 3y agoYes, I was about to suggest that instead of "JS Is Weird", the site would be more appropriately called "My inputs are weird".
- chefandy 3y agoIndeed, 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.
- tylercrompton 3y agoI 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.
- leeoniya 3y agoTypeScript 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.
- ruined 3y agojs being a dynamic language on the web with very "helpful" coercion logic, it is much easier to unintentionally perform such an operation than a non-js developer might expect typescript helps, but i have fired some of these footguns even with typescript.
- hn_throwaway_99 3y agoThat 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/ https://jsfuck.com/
- kerkeslager 3y ago> 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.
- gloryjulio 3y agoBecause 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.
- kerkeslager 3y agoSure, 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.
- gloryjulio 3y ago> 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.
- kerkeslager 3y ago> 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.
- kdmccormick 3y agoSure, 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())
- positron6000 3y agoso 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. valid python: 0*"string"or"other_string"
- kdmccormick 3y agoSure, 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.
- ericmcer 3y agothe top one is... kinda crazy, the second one is very-plausible: `if (name && age) {saveUser()}`.
- gaganyaan 3y agoNo, 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.