The reason for this is because of the floating point standard has a designated sign bit. So there exists a zero and a negative zero in floating point numbers.
Because -Infinity is the identity element of max and vice-versa. Math.max(-Infinity, x) == x. Just like 0 is the identity of addition and 1 is the identity of multiplication.
Due to the fact that adding booleans makes no sense, JS casts all those "true"s into numbers, and because "true" is usually stored as a 1, every true is casted into a 1, and then added.
11) true - true = 0
Again, implicit casting.
12) true == 1
In JS, when you compare two values with different types with "==", you cast one of the values to the corresponding type. As I said earlier, true is casted into a 1, and therefore, this happens.
13) true === 1
The triple equal means that if they're of different type, it will return false, and because a boolean is not a number, it returns false.
14) (!+[]+[]+![]).length == 9
Yeah... Again, I don't know...
15) 9 + "1" = "91"
The sign + means either addition with numbers, or concatenation with strings. Because they're of different type, one has to be casted from a type to the other. In this case, JS casts the number into a string and then concatenates.
16) 91 - "1" = 90
Due to the fact that the sign - means nothing with strings, JS HAS to cast the string to number for that to make any sense, so "1" becomes 1, and 91-1 is 90.
17) [] == 0
Again, implicit casting.
edit: forgot that on reddit you have to line break twice for that line break to be shown once.
Math.max = function () {
var buffer = -Infinity;
for (var i = 0; i < arguments.length; i++) {
if(arguments[i]>buffer){
buffer=arguments[i];
}
}
return buffer;
}
Math.max() starts with -Infinity (the smallest possible value) and then loops over all arguments, comparing them and replacing if newValue>existingValue
Since there are no arguments, nothing ever gets compared to -Infinity. Math.min() works this way too but goes the other direction.
Also, it's the most "neutral" result (there's probably a better term for this), like how in mathematics an empty sum is defined to equal 0, or an empty product to equal 1. It has nice properties like not breaking max(max(X), max(Y)) if X or Y are empty.
Oh yeah, you don't have to give the right number if arguments to a function because functions definitely aren't designed to work with specific arguments
Math.max() starts with -Infinity (the smallest possible value) and then loops over all arguments, comparing them and replacing if newValue>existingValue
Since there are no arguments, nothing ever gets compared to -Infinity. Math.min() works this way too but goes the other direction.
Obviously it should loop on all numbers until it reaches Infinity /s
How the hell is this part of the standard library, and it hasn't been patched? This is a straight up bug, it should return an argument error since it expected 1 or more arguments and it got 0.
It's not a bug, it's documented and actually makes sense from mathematical point of view. As others have pointed out, those are the neutral values for them min/max operations. Mathematical properties like this can be useful for functional programming. Consider you have code like this:
funct = Math.max //in real life, this would be a function parameter or something
funct.apply(null, [funct.apply(null, someArray), funct.apply(null, otherArray)])
It will return the maximum number from both arrays, even if one of them is empty. I think being able to write generic code like this and have it work with built-in functions is nice.
The questionable part here is that casting [] to boolean converts it to true even though casting it to integer or string converts it to 0 or "" respectively, which are both falsey.
Actually, the really strange part is that ![] === false. Because [] == false, keeping it in line with 0 and "" (it's number and string representations). But that means that [] == false == ![].
Ah, that's entirely reasonable...or like 30% reasonable. I'd feel better about it if JavaScript had operator overloading, but I don't want to live in a world where JavaScript has operator overloading.
That's sort of JavaScript's original sin, though. Pretty much all of the horrible things in JavaScript derive from its attempts to just truck past anything a sane language would throw an error about.
It's not so much an issue of static typing, as it is one of JS's original design goals to try to never crash.
Ruby, for example, isn't statically typed, but will quite happily throw exceptions if you try to do things like 1 + "1" or even just "foo" + :bar.
(although it can do implicit conversion if you define the appropriate methods, e.g. to_str(), but thankfully the core classes have sensible definitions)
I actually love JS for that. Types were not up to snuff back then, and you don't want your website crashing constantly, nor do you want to take the time to make a formal proof of your photo gallery widget.
It's quick and dirty and durable. Like a bulldozer. It's a stupid tool for treating a patient, or doing your taxes, or a thousand other things, but if you need to just plow through a problem as fast as possible, boy does it go!
Sure, for its original intended use it's fine, but these things have a tendency to outlive intended purposes.
I'm working on an application at my job that was put in place as a temporary measure that needed to be up and running ASAP and would only be needed for a few months. Many design decisions were made on this assumption. This was 5 years ago, and it's vastly outgrown its original purpose, and it's been a heck of a struggle to bring it up to date
Btw don't take this as me hating on JS – I'm actually really rather fond of it.
First of all, why would you call this function without parameters? Secondly, it actually makes some sense. It basically tries to find maximum value in empty set, which does not exists so it returns lowest possible value.
First of all, why would you call this function without parameters?
A typical scenario is that you have an input list and you want the maximum element of it. You could check yourself if the list is empty first, but it's not unreasonable to expect the max function to have it's own handling for empty lists. -Infinity is the only reasonable (non-exception) result of an empty call to max.
No it shouldn't throw an error. This is the perfect extension to the max function. max function has a simple identity like this:
max(max(...x),max(...y)) === max(...x, ...y)
Explanation for "spread operator" or "...": f(...x) === f(x1, x2 ... xn) for x = [x1, x2 ... xn]
If max() was anything other than -Infinity, or if it would throw an error, that would break this identity for empty arrays (x = [] or y = []).
What you are saying is like non-math people saying "What does 50 mean? You multiply zero 5s together. Nothing is multiplied. You shouldn't get anything. You should throw an error. And 52.53... What do you mean by multiplying 5 to itself 2.53 times?" PS: Someone really said that to me.
Math.max() is supposed to return the max of the numbers provided as arguments, (not the maximum value of that type). So when it’s provided no arguments it returns the smallest possible value
#1: Type of NaN being a number is ok by me. It's maybe a bit counterintuitive, but no more than it being a float, double, etc. is. #2: Yes, this is because of floating point numbers. But not having an integer type is, in my opinion, a pretty bad move. #3-4: Yeah, not a Javascript issue. #5-6: The reason for these is perfectly logical (you start with the minimum possible value as your max value and then check it against each argument), but this is a practical case that produces an unexpected result, and it should be handled correctly. It's this sort of lack of thought that gives Javascript its reputation. #7-9: If you can only add numbers and strings, you should error out when someone tries to do something else. Explicit is better than implicit. It's this sort of bad design that gives Javascript its reputation. #10-11: This can actually be pretty convenient. It's certainly convenient for data science in Python. It's counter-intuitive, but I'm actually kind of ok with booleans being equal to 1 and 0. #12: That doesn't make it a good idea. If a programmer wants to convert types when doing a comparison, they should specify that fact. It's this sort of bad design that gives Javascript its reputation. (And that goes doubly for this fact in Python - this is something that should have been abolished in Python 3.0.) #13: This statement is fine. The fact that this is a late addition to the language rather than the original (potentially only) way to do it is poor forethought. #14: This is a direct result of #7-9. #15-16: Again, it's a lack of forethought in the language design, causing weird phenomena. #17: See #12.
This list was obviously padded with things that aren't Javascript problems, but IMO we're still left with:
Math.max()
Math.min()
[]+[]
[]+{}
{}+[]
true==1 // (or just in general the entire abomination that is the original comparison system)
(!+[]+[]+![]).length
9+"1" // this one on its own is not too bad.
91-"1"
[]==0
These really boil down to:
A bad original implementation of the min and max functions that was kept around for backwards compatibility. (This could actually be extended to bad implementations of a lot of early features that were never fixed - it's the same reason people joke about PHP's mysql_real_escape_string)
Implicit type conversion to strings in cases that really should cause errors instead. (A big part of what's wrong with Javascript is that it tries to plod along no matter how dumb the user's code is. This causes much more subtle errors later.)
A terrible comparison system.
Implicit type conversion to integers.
These are pretty major design flaws with the language, and the image gives examples of these, but then tries to pad it. Which is really what's wrong with the 'loljs' culture - it can't just stick to what's actually terrible about JavaScript.
tl;dr This image sucks. Not as hard as Javascript, but it still sucks.
Being exposed to different paradigms helps a lot with flexible thinking. Trying out things like Erlang, OCaml and Lisp really gets you thinking about the concept of programming itself. However, they take a lot of getting used to. I always recommend C since it's familiar to most procedural programmers.
For Javascript programmers, I usually tell people to try out things that JS doesn't do. For example:
JS doesn't deal with memory at all. C helps you understand memory models better.
JS doesn't enforce any sort of structure. Trying a very rigid language like Java may help you structuring programs.
JS has a very lenient type system. Trying a strongly-typed language like Go, where casting must be explicit, helps you realize where type incompatibilities may lead to issues.
JS's worker model is an odd way to do threading. Something like Stackless Python has lots of different ways to do multiprocessing and allows you to find the method that works for you.
I'll probably think of some other things later. I'll update when I do.
I agree with this. I would probably go for C first, then attempt a functional language. Then when he's learned a functional language, he'll go back to Javascript and be like "damn this language is pretty damn functional".
JS is so weird, because it acts like a procedural language, but so much of it follows the functional paradigm. Everything is made via closures, most of the datatypes (possibly all of them) are immutable, first class and higher order functions, pattern matching (with es6 at least), etc. I think if he learns a functional language, he'll become a better JS dev.
JS has a very lenient type system. Trying a strongly-typed language like Go, where casting must be explicit, helps you realize where type incompatibilities may lead to issues.
Agreed, though I would choose Ada, it's about as strict as you can get, and is very structured. In 2009 when I was at university, the computer science program taught us Ada, when most other schools were teaching Java, and I am so happy they did. Ada is an awesome language. It's what fighter jets and nuclear reactors run on.
Just because you can explain nonsense, doesn’t stop it from being stupid. If I decided to color all the interior walls of my house with crayon and explained to you, “Well, my walls are white and these crayons have color”, does that justify my insanity?
Math.min() returns the least of the given arguments. Imagine and algorith that starts with x=Infinity and loops over the arguments setting x= the lesser of x and the current item. Then return x. If there are no arguments, then the "lowest" value is that implicit Infinity. I don't know if this is a JS thing or a thing with some base C-based Math lib. But calling Math.min() with no args is silly anyway.
Dont make escuses for the dumpster fire that is JS. This is the crap you get when you make a language in 10 days, and also refuse to reject any silly type mismatched operations you encounter.
What makes it worse, is Brendan then spend the next 20 or so years steadfastly blocking any kind of replacement in the browser.
5 and 6 probably have to do with undefined values.
Assuming ‘math.min()’ is equivalent to ‘math.min(undefined, undefined)’, it would make sense to return ‘Infinity’.
Since an undefined number could lie anywhere within a defined domain, it must be a “non-finite” value. You can’t throw an exception, and NaN is reserved for mathematically impossible values. This is a logically incoherent value, but it is still mathematically defined, and therefore must return a numeric result. What we end up with is a call to a function which returns “the minimum of two values which lie anywhere in the real domain.” The only safe answer is infinity.
Javascript does not have distinct data types for integers and floating points, it only has "Number". Therefor, all integers are stored as Numbers and treated the same way as floating point numbers.
In theory, yes, it would be slower for things that only need integers. In practice, there are so many other things that slow JS execution down so much more that this probably isn't a bottleneck worth worrying about.
If you're doing serious number-crunching, you don't use JS — you use something compiled to native code, possibly with the sticky parts written in hand-optimized assembler.
Javascript numbers can be safely used as integers up to 9,007,199,254,740,991 and negative the same amount.
This is over 4 million times larger than the the maximum int32 (2,147,483,647), which is still used all the time for many many environements needing integer values.
In the vast majority of real world cases it doesn't matter that integers over 10 quadrillion don't have exact values.
On the flipside it's easier to divide, square root, and exponentiate integer values without dealing with stuff like 1/2 == 0
Actually null being an object makes sense. But what happens if I call methods on null? I think ObjC is the only language that got it right, calling methods on a null object does not produce a runtime exception. If this doesn’t make sense to you think of it as a beacon that might go offline at any moment, asking it to do anything when it’s offline shouldn’t crash your application, just nothing should happen, until it is back online again and it can respond.
I haven’t programmed in all languages but calling methods on null (or sending messages to nil in ObjC lingo) is perfectly valid in ObjC and makes a lot of sense. I won’t do it in C++, Swift or Java, but ObjC allows it and is part of its dynamic nature.
That one's not too bad (although it would be better for it to have, for example, a nulltype). typeof undefined is a bit weirder, but should never actually be an issue.
Sorry, I disagree. I can understand the difference between == and === because of the dynamic types, or adding booleans again because of the dynamic types. But some of those are borderline ridiculous and downright unintuitive. For example many bugs are introduced by unsuspecting programmers by adding an int with a string since everything in the DOM is a string. A very common break point of Javascript code.
'Quirky' is fun sometimes, but this is over the line. Defending languages which do this will make sure we get this in the next generation languages, which nobody wants. I get this language was designed in a very short period of time, but excusing is terrible behaviour is unacceptable.
Am I the only one getting annoyed with these posts. Of course, when you do things like adding arrays, weird behavior happens. But how often do you write code that uses this undefined behavior.
The post isn't the problem as it is in ProgrammerHumor subreddit, where it belongs. The problem is people on both sides of the aisle who treat it as a serious attack of JS.
I think they're all fine as long as it's done in an origional way. This post just seems like a laundry list of all the other JS memes combined into one.
Probably not very often but you could have an issue if you use poor naming conventions and reuse of local variable names. If you mistype the variable identifier and don't notice, it's possible JavaScript's type coercion would make debugging a little more painful. All that said, it is kinda annoying to see posts about "omg you can do this thing that doesn't make any sense with JavaScript", like yeah we get it, JavaScript is weird.
Linters help but they're there to work around the weak parts of the language. Similar things are IDEs with good function name and argument order hinting for PHP. They do help you with PHP's inconsistent standard library function names and argument orders, but why is this a problem that exists in the first place? My opinion.
I think there is a serious philosophical debate underlying the memes. The question is should a language like JavaScript try so keep your code executing even when it doesn’t make sense. I personally think it should since it makes it much harder to find wired random errors. I prefer a system like Typescript that forces some type discipline.
True==1 and True===1 are obvious if you understand what == and === mean. == means that they get casted then compared, so true gets casted to a number, while === demands that they're the same type of variable.
Exactly, the only thing you could argue is whether those two operators should be swapped around (so '==' behaves like in Python and '===' behaves like PHP's/JS' '==') but they certainly have their place.
No, they're saying "symbols should do nothing at all when there is not a self-evidently correct action". Adding a number and string should be an error. Comparing two completely different types should be an error.
Just because you have a spec that completely defines all possible behavior does not mean that the spec is any good. If I created a language where adding any string and integer together returns the string representation of the ackermann function of that number, would you consider that a good language? (If you don't know what the ackermann function is, suffice to say that for any input greater than 4 it will never finish computing and need more RAM than you could ever have to represent.)
To the people saying Javascript should throw errors rather than carry out poorly defined instructions, here's my two cents. As a language, Javascript was designed to give some sort of result even in situations like these where the operations are vaguely meaningful at best. Whether this was a good choice is definitely debatable, but seeing these examples in the light of that design choice at least clears things up a bit in my opinion. Even as someone who enjoys using the language, I definitely agree this behavior poses a huge potential problem, but at the same time I would say that in my experience most of these behaviors rarely come up and are about as hard to debug as, for instance, an unexpected null pointer in another language.
WITHOUT JAVASCRIPT YOUD BE MAKING WEBSITES IN VBSCRIPT, FUCKING REALIZE THIS AND STFU ABOUT MINOR JAVASCRIPT ISSUES THAT ONLY NOOBS WHO THINK PROGRAMMING IS MAGIC COME INTO TOUCH WITH.
But it's bloody not all the same. Choosing a better / more appropriate language for a given project can massively increase productivity and reduce bugs / friction.
Well, some people don't care about that. I present to you Node OS. I'm not sure why but I admit I haven't spent much time studying it. So let's say I'll give it a small benefit of the doubt.
it's absolutely javascripts fault for being a bad language with behavior that easily leads to bugs rather than errors. it's the programmers fault because they are presumably the ones that chose to do whatever it is in Javascript in the first place.
I believe I read somewhere that one of the reasons JS was built like this was so that it would crash a site just because some bit of code failed. For example if you had a bunch of code for a page but the code for some ad written by a different programmer included a bit of code that did an illegal operation. In most languages the whole app would crash unless that exception was handled properly. JS was built to try to keep going even if it encountered some should-be-exception-throwing code so that's why it tries to coerce types and that's why you get weird cases like these. Not that that justifies this but maybe that provides insight into why JS is like this
I also like my programs not crashing so I run all my code in ring 0 to avoid segfaults. I much prefer the unexpected side effects of code doing things I never intended.
Honestly this sub should just be renamed shitting on JS though. Seems like half the posts ever are from people just encoutering the language seriously for the first time and saying "LOL THAT'S SO DUMB". It might have been funny a decade ago but now it's trite, unfunny, and boring.
My point exactly. It's getting repetitive, and old. The "lol. wut?" talk about JS things was YEARS AGO. It was funny then. Now it's boring, because you don't have anything new. Also most of the issues being presented are contrived and never actually used in real code.
I agree that some of these have no real purpose IRL but I also think you underestimate the amount of bugs some of these cause in the real world. A notable mention is adding vs subtracting an integer with a string. Especially since everything tends to be a string in the DOM.
Sure, it doesn't complain if you forget to parseInt or parseFloat, but that's like the spectator asking Charles Babbage: "If you feed it the wrong input will it give you the correct output?"
Just... figure out what you're doing. Escape your inputs, parse your inputs, validate your inputs, etc. It's a basic requirement for everything web.
I certainly agree with what you're saying, writing PHP and JS daily for a few years taught me those lessons the hard way. I hit my head countless times against quirky behaviour. I admit it's second nature now, but I just think it's a bit dangerous when such matters are promoted as 'normal', as a matter of just knowing the language deeply.
It might have been funny a decade ago but now it's trite, unfunny, and boring.
Oh shit, that's "peak language" talk .. someones going to try to create a replacement where all the next generation of hipsters can go too :P
Or just implement any possible language on top of the existing one. I feel like this has got to be a JS exclusive lifecycle event. the shear number of languages you can compile to JS is crazy
While some of those make sense, I think you're an even bigger idiot for insulting people and defending actual bug sources like like 9+"1" vs 9-"1", especially considering everything in the DOM tends to be a string.
I get it was designed in a short period of time but that doesn't give you an excuse to pretend it's good. I write PHP and JS daily and you don't see me pretending that they don't have problems. Grow up.
You mean to say that the functions that return the maximum or minimum of one or more numbers will return something weird (but described in the docs) when you don't give it any numbers to work with?
Would you expect sum() to return undefined, throw an error, or return 0 (the empty sum)? Would you expect product() to return undefined, throw an error, or return 1 (the empty product)?
In both cases I would prefer the latter. This allows me to call them with empty lists and get sensible outputs. If an empty list is not valid for my use case I can check that myself, but for many use cases it is perfectly valid. max() and min() are no different. -Infinity is the empty max, and Infinity is the empty min.
So you're telling me that functions that determaine the minimum and maximum values of two numbers return something weird when you don't give it any numbers? Really makes you think.
(Also btw, if one or more of the parameters aren't numbers you get NaN, which makes pretty good sense tbh)
This happens in any language with NaN. NaN is a special floating point value reserved for the result of invalid math on floating point that wouldn't result in Infinity. JavaScript also happens to use it when converting a string to a number and it fails to convert.
JavaScript uses floating point for all numbers (hence the ability to use NaN when converting strings). So you get limited precision. In a modern language when using this number it would not fit in a 32-bit integer so you'd have to use a 64-bit one. JavaScript uses 32-bit floating point so you're gonna lose something.
.
Roading error, because again floating point.
Math.max returns the highest value passed to it. Passing no values results in undefined behavior. In this case, it probably uses -Infinity as a highest value before examining values passed into it so it can handle any possible values properly.
Same deal.
Adding arrays does not make sense, so JavaScript tries to convert the arrays to some type where adding them would make sense. An empty array converts to the string "". Yes, this in and of itself is a bit of a WTF, but it comes with the weak-typed nature of JS, which is the root problem imo. Gotta use TypeScript or something and strong type everything to avoid these pitfalls.
Same as above but JS decides a number type makes more sense. It just depends the order in which it goes through type conversions. In the end, it boils down to garbage in, garbage out.
This happens in any language, just that strongly typed languages require explicit conversions. Can't add booleans so they get converted to numbers. true will get converted to 1 in pretty much every klanguage except BASIC where they made it -1 for some reason.
Same.
Same, also see the next item.
=== compares types so the conversion recognizes the mismatch where == tries to convert types to get things to match. In fact, you should be using === instead of == unless you have an existing code base that relies on implicit type comparisons and conversions to function properly. JS can do explicit conversions by passing stuff into Number() or Boolean() or String() etc functions.
All the stuff in the parenthesis ends up converted into a string in order to add it together, which has a length.
Can't add a number and a string, so JS has to guess. You can't always convert a string to a number, but you can always convert a number to a string, so it makes the 9 into a string before adding.
Unlike + there is no string operation for - so JS is forced to convert the string into a number in this case for the operation to be valid.
Same as earlier, when [] is forced to a number it becomes 0 as it is empty.
If you want a REAL WTF... Function is a class which represents any function in your code (or any API too). Function itself has functions on it which are instances of Function. Have fun figuring out how that is possible.
Most of them which might be considered unreasonable are the most reasonable and logical sentences exist on the planet. Buy you have to take a look b6 a different sight.
Could JS just add strong typing in a future version? Optionally, at least.
There could be a flag to turn off implicit coercion for everything (except maybe casting numbers to strings, and ints to floats) and make == semantically identical to ===. All operations with incompatible types would throw.
The real reason to thank him is not for the shitty language choices, but for all the malicious code that enables a billion dollar revenue model via tracking and advertising.
Or do we thank the web browsers for that gem by allowing JavaScript on the browser to begin with.
5
u/GoldenWillie Jun 21 '18
Here’s another one for you
-1*0 -> -0
The reason for this is because of the floating point standard has a designated sign bit. So there exists a zero and a negative zero in floating point numbers.
2
u/sparksen Jun 21 '18
Okay Somebody explain Math.min and .max Like come on that should be at least min=-inf and max=inf
690
u/hectobreak Jun 21 '18 edited Jun 21 '18
Ok, here we go:
That is not because of JavaScript, but because of the IEEE 754 standard for floating point numbers.
2) [large number] = [another large number]
This one is, again, due to the floating point standard.
3) and 4)
Again, floating point. This time it's a floating point rounding error.
5) and 6)
yeah... I've got nothing.
7), 8) and 9)
http://2ality.com/2012/01/object-plus-object.html
10) true + true + true = 3
Due to the fact that adding booleans makes no sense, JS casts all those "true"s into numbers, and because "true" is usually stored as a 1, every true is casted into a 1, and then added.
11) true - true = 0
Again, implicit casting.
12) true == 1
In JS, when you compare two values with different types with "==", you cast one of the values to the corresponding type. As I said earlier, true is casted into a 1, and therefore, this happens.
13) true === 1
The triple equal means that if they're of different type, it will return false, and because a boolean is not a number, it returns false.
14) (!+[]+[]+![]).length == 9
Yeah... Again, I don't know...
15) 9 + "1" = "91"
The sign + means either addition with numbers, or concatenation with strings. Because they're of different type, one has to be casted from a type to the other. In this case, JS casts the number into a string and then concatenates.
16) 91 - "1" = 90
Due to the fact that the sign - means nothing with strings, JS HAS to cast the string to number for that to make any sense, so "1" becomes 1, and 91-1 is 90.
17) [] == 0
Again, implicit casting.
edit: forgot that on reddit you have to line break twice for that line break to be shown once.
65
u/Bill_Morgan Jun 21 '18
JavaScript has a very interesting type system
42
u/flipperdeflip Jun 21 '18
Not bad for a 10 day hack project, being an essential cog in the machine of modern human civilization.
47
u/[deleted] Jun 21 '18
How the fuck do you trash on JS when you don't even understand the point of == and ===?
80
u/DeBarco_Murray Jun 21 '18
Obligatory:
https://imgur.com/a/oe6ZX
50
u/Ericakester Jun 21 '18
And it's your fault if you do any of these
49
u/[deleted] Jun 21 '18
Am I the only one getting annoyed with these posts. Of course, when you do things like adding arrays, weird behavior happens. But how often do you write code that uses this undefined behavior.
7
u/justingolden21 Jun 21 '18
True==1 and True===1 are obvious if you understand what == and === mean. == means that they get casted then compared, so true gets casted to a number, while === demands that they're the same type of variable.
18
u/Mozai Jun 21 '18
Some of these are " symbols should do what I guess, not whatever is written in the Javascript spec." C'mon, that's a student mistake.
That sixteen nines tho, I can't defend that.
6
u/TSLRed Jun 22 '18
To the people saying Javascript should throw errors rather than carry out poorly defined instructions, here's my two cents. As a language, Javascript was designed to give some sort of result even in situations like these where the operations are vaguely meaningful at best. Whether this was a good choice is definitely debatable, but seeing these examples in the light of that design choice at least clears things up a bit in my opinion. Even as someone who enjoys using the language, I definitely agree this behavior poses a huge potential problem, but at the same time I would say that in my experience most of these behaviors rarely come up and are about as hard to debug as, for instance, an unexpected null pointer in another language.
11
u/Adrepixl5 Jun 21 '18
REEEEEEEEEEEEEEEE
97
u/[deleted] Jun 21 '18
WITHOUT JAVASCRIPT YOUD BE MAKING WEBSITES IN VBSCRIPT, FUCKING REALIZE THIS AND STFU ABOUT MINOR JAVASCRIPT ISSUES THAT ONLY NOOBS WHO THINK PROGRAMMING IS MAGIC COME INTO TOUCH WITH.
8
u/wooglin1688 Jun 21 '18
if you have trouble using javascript for the supposed reasons listed in this post you’re a moron.
4
u/mikeputerbaugh Jun 21 '18
Thrown-together languages like JS, PHP, and C make the world work
15
u/inabahare Jun 21 '18
You mean to say that the functions that return the maximum or minimum of one or more numbers will return something weird (but described in the docs) when you don't give it any numbers to work with?
1
u/sujith315 Jun 22 '18
Details for 3 and 4 please
9
u/Cilph Jun 21 '18
For the last fucking time people, this is IEEE standards.
7
u/davidbrit2 Jun 21 '18
Welcome to Javascript, where everything's made up and the points don't matter.
2
u/[deleted] Jun 21 '18
Python3 has the same issue with .1+.2 == .3
3
u/blackmist Jun 21 '18
JS: Because who needs exceptions or syntax errors?
1
u/The_MAZZTer Jun 21 '18 edited Jun 21 '18
If you want a REAL WTF... Function is a class which represents any function in your code (or any API too). Function itself has functions on it which are instances of Function. Have fun figuring out how that is possible.
1
u/[deleted] Jun 21 '18
you forgot batman
1
u/[deleted] Jun 21 '18
-Infinity1
u/Liesmith424 Jun 22 '18
This is why I stick to INTERCAL.
1
u/EXTRAVAGANT_COMMENT Jun 22 '18
testing equality between floats can go fuck itself. in every language.
I have had to make an application where:
I swear to fuck you could enter 3.0 then enter 3.00 and it would recognize that they are different, but only on Tuesday.
1
u/[deleted] Jun 22 '18
A lot of these are intentional.
1
u/[deleted] Jun 22 '18
My name!e is Brendan. I feel attacked.
1
u/bapcs_data_bot Jun 22 '18
You can make some nonsense looking stuff in any language, too
or, one taken from /u/dbader's book (though I think he mentioned he initially saw it somewhere else)
a final one from his book, just cause I think it would make a hilarious joke embedded in a buddy's code somewhere
I referenced a couple things, so I think its appropriate to link his book
1
u/CharaNalaar Jun 22 '18
Number three doesn't make any sense. 0.1+0.2=0.3, no doubt about it...
1
u/Morrido Jun 22 '18
Only 5, 6, 7, 8, 9 and 14 are actually JS warts. The rest are things JS got from somewhere else.
1
u/aiispassw98 Jun 22 '18
Most of them which might be considered unreasonable are the most reasonable and logical sentences exist on the planet. Buy you have to take a look b6 a different sight.
1
u/Buttercak3 Jun 22 '18
So... Have you ever programmed in any other language that has floating point numbers?
1
u/PatPL Jun 22 '18
My personal favourite
1
u/Arancaytar Jun 22 '18
Could JS just add strong typing in a future version? Optionally, at least.
There could be a flag to turn off implicit coercion for everything (except maybe casting numbers to strings, and ints to floats) and make == semantically identical to ===. All operations with incompatible types would throw.
1
u/_PM_ME_YOUR_ELBOWS Jun 22 '18
It just keeps getting better
1
u/lenswipe Jun 23 '18
The
true === 1being false is actually reasonable. You're comparing a boolean with an integer.1
u/sharoonj12 Aug 27 '18
Watch Advanced Javascript Course it will help you alot https://www.youtube.com/watch?v=o9nnYb8lNdQ&list=PLAET2V7ZwO6dq0wIqdnRIZtXWVuRzDOWM
-1
u/DrFloyd5 Jun 21 '18
The real reason to thank him is not for the shitty language choices, but for all the malicious code that enables a billion dollar revenue model via tracking and advertising.
Or do we thank the web browsers for that gem by allowing JavaScript on the browser to begin with.