JavaScript is notorious for its inconsistencies in a lot of areas. It's also powerful and popular and has a lot going for it.
But can we poke fun at weird things? (whether or not the behavior is a good thing in general)
Let's start with an example:
+'a'
resolves to NaN
("Not a Number") because it coerces a string to a number, while the character a
cannot be parsed as a number
document.write(+'a');
nan
.
Adding NaN
to "ba"
turns NaN
into the string "NaN"
due to type…
Top comments (85)
I love this. It just made me think about something pretty interesting... If you think of this in terms of what it actually could mean for a mathematical function, then the string appendage (regarding integers) could be thought of as its own math operation too - in a very round about way.
Absolutely, and not even in a very roundabout way, you're pretty spot on :)
Question: should we (as JS coders) assume that people know this, and not hesitate to write this sort of code? or should we refrain from this and use explicit type conversion ...
(well the answer is probably that if it is 100% clear that the data type of the first operand is always string and the second operand is always number then this would be okay - if in obscure corner cases it can sometimes suddenly be different then good luck debugging/troubleshooting ...)
😂😂
JS coercion at its best and worst in two lines lmao!
This took an hour of my life:
omg thank you for your service as I really didn’t know about this.
yep, a good check for an actual object is
Kind of insane they did that that.
It's not insane.
null
, likenil
in ruby, a singleton instance of NilClass is technically an object.Technically an array is also an object, which is why there is Array.isArray (which has its own caveats).
But at least ruby is rigorously consistent OO like that.
In JS there are very few object-type calls that work in any way with nulls, so it's neat that they aimed to make this aspect of the language more OO, but they should have followed through better.
typeof(null)
being 'object' is seldom anything but annoying for JS devs.eg.
Basically there are very few times you might write
typeof(thing)==='object'
where you shouldn't do the null check tooAbsolutely true. I was only referring to the use of the word "insane" which just sounds very unknowing/ignorant to me.
FWIW, now that we use TS for most things, this is not problem for us often, except for when something is nullable ánd multiple types (object/number/string/undefined).
My recommendation is to never use
null
and only useundefined
. Replace your "null"s with an "EMPTY/UN_SET" data type.Fair enough. I didn't mean insane in the sense of completely without reason, just in the sense that it was badly thought out.
The thing is that nulls are now baked into JS at a few levels, so in our own code we can avoid them, but they pop up throughout the various JS apis, eg
That's what I mean by the loose "insane" comment. It was a mistake that was bad enough that as JS devs we're now best off to generally avoid the fundamental null value in JS.
I totally agree with you about TS, which corrects many of these issues, and how in JS we're best to use
undefined
whereever possible.Or
Object.prototype.toString.call(thing)
Without a doubt I'll say this: jsfuck.com/
My work firewall thinks this is porn.
Ryan, HR needs to now why you're searching for programmers porn during work hours
Clearly :D
check out the source for a full list of the nonsense they leverage. I feel like "explain each line of this file to yourself" is probably a pretty good JS litmus test.
Wat is a funny talk about odd behaviors. JS starts at 1:22.
A colleague the other day noticed that the
setTimeout
/setInterval
takes a32 bit signed int
as an argument, which is fine except that (as far as I know) JS doesn't natively support integers... but but instead Numbers are specifically8 byte signed doubles
. If you happen to have a interval or timeout every2^31
miliseconds (ish) or more, then the value is interpreted as a negative number and therefore executes instantly and in terms of intervals repeatedly. So timeouts and intervals are limited to just under 25 days.I'm guessing a web app won't need a 25-day timeout or interval.
I tried a very basic test on node and it seems to be waiting on it to execute... any idea why?
not sure... i just ran it in node and experienced the same issue as the browser. The following line should make node print without pausing:
or
where the following will take 596.5 hours (roughly):
I am currently on a Windows machine running v8.9.1, perhaps its solved in later versions?
I think most of the people get confused by the language's type coercion rules and operations related to those, which are quite often not intuitive:
The only thing that surprises me here are the loose equality comparisons between object and array; but it's not as though any sane programmer would be doing this. Otherwise almost all the others look fairly intuitive: coercion is applied to satisfy the operator being applied.
It's perhaps more useful to understand all the values that can be coerced to false; and why sometimes it's a really bad idea to use a shortcut
if(isSomeValueTruthy)
style condition...edit - actually
[] + 1
is weird :DWow, this is a new one for me. The array coercion actually caught me off guard.
Yeah, this is a good one. Most languages try to coerce only one side of an expression at a time, and won't ever coerce collection types to non-collection types.
My favorites...
Most languages do this, but it could be a whole lot worse (fun fact, IEEE 754 actually allows for (2 ^ N) - 1 distinct
NaN
values not counting signedNaN
values, where N is the bit width of the significand field in the binary representation (so for the 8-byte floats JS uses, there are 18014398509481982 possible NaN values including both signed values)).The reason is simple,
NaN
is a numeric representation of a non-numeric value, so by definition it has to be the same type as numbers for the language (or, at minimum, it has to be at least one of the numeric types in the language), but you can't be certain what value it is, so you can't treat twoNaN
values as being identical.Encountered this one yesterday and spent a day on it.
A
File
object cannot be serialized into JSON. There's a StackOverflow question about it.But essentially if you do
The browser will happily return something like:
But doing
will result in:
With no properties. I've noticed that it does this for object spread as well so if you wanted to copy File info, you can't just do:
{ ...someFile }
, you'll need to call and pick out the individual properties manually.This made my day 😂
Imo the implicit type conversion is a little weird, but even ignoring that, I think the point was the difference in behavior between
'11' + 1
, which converts the number to a string, and'11' - 1
, which converts the string to a number. This discrepancy makes implicit type casting feel unpredictable and weird, at noon least to me