Live data from Hacker News

JavaScript Is Weird

jsisweird.com

231–240 of 383 posts

Re: JavaScript Is Weird

#231
TypeScript to the rescue! I just prefer strongly typed languages, but it's things like this that disappear (for the most part) when you have types. I personally can't keep types in my head as well as some, so when the language forces me to, it helps a lot. Vanilla JS just makes me feel like I'm walking on egg shells.

Then we can get into a lot of fun when we get to type systems like SML and Rust where you start to really touch on some power (but can become intimidating). I'm still a bit weary of trying to hop into Haskell.

Re: JavaScript Is Weird

#232
post #133
post #97

I stopped at the first example (true + false). If you write code like this, you have bigger problems than using JavaScript. For most of the examples, it's like saying "Here's what happens when you plug a fan to a faucet". There's no point really. JavaScript is weird just like any other language can be weird when you use it in weird ways. And yes, it's certainly one of the weirdest. If you want to talk about things we…

> I stopped at the first example (true + false). So you never did something like this: x = performCalculation(getParam1()) y = performCalculation(getParam2()) sum = x + y only to discover that "performCalculation()" sometimes returns a boolean instead of a number? JS is a dynamically typed language after all and functions like function f(x) { if (x >= 0) { return Math.sqrt(x) } else { return false } } are perfectly v…

some people will be quick to say "oh but then the library is just awful". Yes. Yes it is. Sometimes one has to work with brain-dead stupid libraries, and in those cases, it'd be much easier if JS would just give you an error saying what happened: You're trying to multiply apples with oranges.

Re: JavaScript Is Weird

#233

Earlier quoted context omitted.

I think the situation is a bit different. This situation looks different in reality. The result may be (null-0)+"0", but the actual code will be foo()-bar()+baz(). And C will at least give you warning about types, even if NULL-0+"0" could give you an address. Plain JS without extra tooling would happily give you the unexpected result. Some other dynamic languages would at least throw an exception about incompatible t…

Yeah guess this is where Typescript comes in

TypeScript wont magically fix type errors. It is not that hard, to sanitize any expected parameter for functions and their output, typescript wont do that for you. So if you typecheck i/o values per se, using typescript only slows down dev process, as this can be easily done in vanilla javascript. no need for more bloat, but only for some defensive programming.

Re: JavaScript Is Weird

#234

These examples are amusing but irrelevant. What I find sorely lacking is sane syntax for working with arrays. Eyeing Python with envy every time. And some pattern matching would be nice too.

Special syntax for specific data structures are exactly why I hate so many languages. No. We don't need more of that garbage. Map & co. do everything you need and adding yet another syntactic construct on top of it would just break the language even more.

Re: JavaScript Is Weird

#235

Earlier quoted context omitted.

I understand where you are coming from. But the addition operator simply does not have any special handling of arrays. The specification [1] clearly defines what it should be used for: "The addition operator either performs string concatenation or numeric addition." As JavaScript is weakly typed, it is the programmer's responsibility to use proper value types with these operators. That limitation (or advantage?) is a…

It seems that by "expected", you mean "expected, by anyone who read the spec" which I don't think is a fair use of that word. Obviously, most JS developers have not and will not read the spec. I am very happy with JS and TS and I think the coercion rules are easily worked around with linter rules and policies, but they are definitely weird and I think the language would be better if it simply threw exceptions instead…

No, I meant "expected by anyone who learned the language". Knowing the addition operator including its limitations is quite basic. I'm not saying you need to be able to solve all this "JavaScript is weird" puzzles as they are mostly non-sense. But you definetely have to know what you can us `+` for.

If someone does not like the ECMAScript specification, that is fine. But at least use a proper unofficial documentation like MDN.

Re: JavaScript Is Weird

#236
post #19

0.2 + 0.1 === 0.3 That's not really a JS problem, that's a floating point problem. Plenty of languages will have the same issue. +!![] "" - - "" (null - 0) + "0" Calling these things weird is fair enough but I can't help thinking this is code you'd never actually write outside of the context of a "Look how weird JS is!" post. It's like picking examples from the Annual Obfuscated C Contest to show how hard it is to un…

Someone had linked on here a website that showed the 0.3 thing in practically almost every programming language and their output. I wish I could remember the domain / url cause it is interesting to compare languages defaults. I know most languages have ways to handle it correctly.

https://0.30000000000000004.com/

Re: JavaScript Is Weird

#238
post #75

Earlier quoted context omitted.

That’s a load of gibberish. JS as a language has nothing to do with browser APIs, why would you judge it based on that? Also, ES6 was ratified six years ago. Seems like you had a bad experience with espruino / jerryscript or something and are projecting based on that? Dynamic languages are easier to program in. That’s a fact, and why they are so popular.

gibberish? "JS as a language has nothing to do with browser APIs, why would you judge it based on that?" Excuse me? That is precisely what JS is based upon. Lemme give you a prime example: JS = a single threaded event loop, ON PURPOSE. That directly affected Node.js as an implementation. Your browser has an embedded scripting engine. Embedded, meaning, the source code for your browser includes the scripting language…

Though there are C compilers and also C code generators written in Lisp.

Re: JavaScript Is Weird

#239

Earlier quoted context omitted.

Presuming server-side JS, as you won't run Ruby or Python in the browser, typically, you can say that all 3 dynamic languages, as such (garbage collected, heap-based, untyped) will waste computer resources and perform worse than typed compiled languages (even garbage-collected ones) due to memory usage patterns. Python has an interesting C FFI interface, among other approaches, that can at least allow CPU and memory-…

> As I see large front-end teams frequently pushing JS from a typescript base lately, I'm not sure that even the JS community supports "everything JS" anymore. Given how close typescript is to javascript, wouldn't it be more appropriate to treat it as just a dialect of javascript (like coffescript was) rather than a different language like python or go would be?

In the sense that there's extra meta information (type) it can be used as a frontend for other things than JS, so I could easily see

1) TS to WEBASM (once JS is out of the picture, might be awhile, lol...)

2) TS as an LLVM IR frontend (compiled TS on the server)

which is basically predicated upon industry familiarity as it's selling point, so yeah, I could see where it's also just a JS industry side-effect. A wart remover, lol

Re: JavaScript Is Weird

#240
post #224

Earlier quoted context omitted.

Which is why we use Typescript

Typescript doesn’t save you from all the weird things happening at runtime. A missing check at a context boundary and you can have a wild time (been there).

Context boundary meaning where it interfaces with javascript?
Post reply on HN