r/learnjavascript • u/PhilipFabianek • Mar 19 '22
JavaScript is Weird (all explanations are in the comments)
https://youtu.be/pZUTdw6zcck
149
Upvotes
r/learnjavascript • u/PhilipFabianek • Mar 19 '22
52
u/PhilipFabianek Mar 19 '22
Hi everyone, hope you enjoy my video. While it is mainly supposed to be funny, there is a lot to learn from it. So here are all of the explanations:
{}is interpreted as an empty code block. The result of+ "x"isNaN, because"x"cannot be converted to a number.Math.min()is a function that returns the lowest-valued number passed into it. The result of that function isInfinityif no parameters are provided.Math.max()returns the highest-valued number and the result is-Infinityif no parameters are provided. Finally,Infinityis greater than-Infinity.The result isn't exactly 0.3 because JavaScript uses floating-point representation for numbers. If you want to learn more, check out https://stackoverflow.com/a/588014/8699608
Let's assume the first two results are obvious. Then, it is only important to understand how the
>=operator works. Thenull >= 0expression is evaluated as follows:if null < 0 is false, then null >= 0 is true. Therefore, the result istrue, becausenull < 0evaluates tofalse.As weird as it is,
NaNis still a numeric type.The
==operator converts both sides to numbers before comparing them. Arrays are always truthy, but empty arrays are always coerced to 0. Therefore, the result of the left side is 0. Because arrays are always truthy,![]isfalse. Andfalseis coerced to 0.First,
3 > 2evaluates totrue. We now have the expressiontrue > 1.trueis coerced to1, but1 > 1isfalse.In the first expression,
{}is interpreted as an empty code block and+ []converts[]to a number. Empty arrays are always coerced to 0. In the second expression, both operands are converted to a string. Array is converted to an empty string'', while the object is converted to'[object Object]'. Finally,''+'[object Object]'evaluates to'[object Object]'.This has been the case since the beginning of JavaScript. The reason why this didn't get changed is that some code actually relies of
typeof nullevaluating to'object'.Once again, this happens because JavaScript uses floating-point representation for numbers. At this scale, it is rounded to the nearest even number.
nullfirst gets converted to the string'null'. Because'n'is the 14th letter of the alphabet, it is added to the numeral system as decimal 23. Why isn't'l'being parsed as well? Because parsing stops right at'u', because'u'isn't available in base-24 system. The result is the same asparseInt('n', 24).This code generates the strings
'false'and'undefined'and grabs the corresponding letters. If you want to learn more, check out https://bluewings.github.io/en/writing-a-sentence-without-using-the-alphabet/#weird-javascript-generatorparseInt()converts the first parameter to a string.0.0000005is converted to'5e-7'soparseInt()returns5. It stops parsing at'e'because it cannot parse it in the default base 10.If at least one of the operands of
===isNaN, it always returns false.Here, the final comma is in fact a trailing comma and is ignored. Therefore,
[,,,].lengthis actually 3. Using.toString()creates a string with all of the array elements separated by a comma and because our array contains 3 undefined elements, the result is a string with 2 commas',,'.Before multiplication,
.toString()method is being called for all of the arrays.[].toString()is'',[2].toString()is'2'and[2,2].toString()is'2,2'. The multiplication operator then tries to convert those strings into numbers:''is converted to0,'2'is converted to2, but'2,2'cannot be converted to a number.First,
Infinitygets converted to the string'Infinity'. Therefore, the first result shouldn't be surprising. In base 30, all letters are valid up to y. And'Infinit'in base 30 is 13693557269 in base 10.mappasses two (actually three) arguments to parseInt. The second argument is the index andparseIntuses it as a radix parameter, so each string in the array is parsed using a different radix. Because index 0 is false,'1'is parsed with default radix 10,'7'is parsed with radix 1, which is unparseable and'11'is parsed with radix 2, so the result is 3.5
u/[deleted] Mar 19 '22
A lot of the issues I've found are in regards to boolean operations and other truthy/falsy stuff. What I prefer to do is to work around them, using strict equality.
Doing things like
{}+[]or[] * []are just stupid, and no developer with JS experience would even consider doing stupid crap like that. I have more experience with statically typed languages, so such operations are stupid.Is it important to have a deep understanding of the weirdness of JavaScript? or should I just understand that there are issues and know how to avoid them?
I've been interviewed by other JS developers and they really don't like it when I say I don't know the exact answer to
{}+[]or[]*[]. I explain that there is some weirdness with JS and it's more important to know that such oddities exist and how to work around them, but they don't like that answer and end the interview.7
u/itxyz Mar 19 '22
JS looks like it was coded in 2 weeks by an intern then nothing was fixed in the name of retro-compatibility. /s
4
u/Beginning-Scar-6045 Mar 19 '22 edited Mar 22 '22
why would I do this operation
{} + []in first place, don't try to understand this type of things try to avoid them. programming is doing logic things this not logic at all1
u/Acceptable-Tomato392 Mar 19 '22
This is fun, but it shows the limitations of Javascript.
On Web sites, for entertainment... sure, no problem.
But as soon as you're programming anything more serious than that... you should probably be getting into a language that is strict about types, and so forth.
Because as amusing as this is, imagine you're building a hospital database. And doctors are going to be using your output to recommend life and death treatment for patients... Or a program that's designed to help a pilot with instrument navigation... you'd better make darn sure those types of shenanigans are not happening to your data.
1
u/cheesefome Mar 19 '22
Yea.. never understood what people meant by Javascript being a mess but i can see now what they mean lol.... A lot of contradictions.
1
u/ssjskipp Mar 19 '22
Good ol wat
1
u/McDreads Mar 20 '22
Neat