In this talk I will try to show you what the language has been trying to tell us for all these years, where the real path to enlightenment is, or at least we can have fun and look at things we know (or pretend to 😉 from an unorthodox point of view. 😉
Something we’ve always enjoyed in the aftermath of Web Directions conferences is reading the blog posts and articles by attendees that follow. There’s always half a dozen or more, and they act not only as record of what took place that’s useful to those who didn’t attend, but also as a kind of a filter that lets us see our events through others’ eyes.
Most people outline broad conference highlights or focus on a handful of the presentations and the insights gleaned from them, but for many years now there has been this one post-conference blog post we’ve all come to look forward to: Ben Buchanan’s Big Stonking Post™.
Partly in acknowledgement of all those people who write about their experiences at our events, and partly in admission that our usual Wrap format just wouldn’t capture Dmitry Baranovskiy’s talk, we’re turning this one over to Ben, re-published with permission from the 200ok weblog.
Those who are unaware they are walking in darkness will never seek the light. – Bruce Lee
Zen has the well-known conundrum of ‘what is the sound of one hand clapping’… in JS the question is probably ‘what number is not a number’. We are afraid of NaN!
What if NaN is there for a reason? What if we used it? If we have a function that returns a number but returns null if there’s an error… why not return NaN instead of null?
People are afraid of NaN because the first time you get NaN it’s because you fucked up. So from then on you think if you see NaN you did something wrong.
Accept NaN. It’s there. It exists. You can use it.
Dmitry used NaN in real code twice last week… he’s not saying his colleagues appreciated it.
== is hated… why so much hate? Dmitry’s current team has a rule you cannot use ==. There are reasons for it. There are unexpected and confusing results when you use ==. So we use ===.
=== is a true friend, always telling you what you want to hear.
But === is not fixing the problem, it’s covering the problem.
What about <= >= < > …? Not using these is like hiding by covering your face. If you don’t understand what you’re doing, you are in trouble.
.charAt() doesn’t care what you pass in to .charAt(). Be like .charAt(). It always returns something. Exceptions? Why throw exceptions? Why be so rude?
It doesn’t care what it’s running on, either. Pass it a number, it returns a number. Pass it NaN, it returns a character from NaN.
NaN.charAt() → “N”
Undefined isn’t even null. It’s more empty than null.
There is nothing new.
RegExp() and new RegExp() → the result is the same. Function() and new Function→ the result is the same
But then you have things like:
Date() is not equal to new Date(). Same for Number(), Set(), Symbol().
The five faces of function.
You can use Function five ways. You can call a function, you can return a function, you create contexts and closures with functions, you can use function as a constructor, you can call it with strings or numbers.
Is it a good thing, or a bad thing? Dmitry doesn’t know. But it’s how the language is written.
And those who were seen dancing were thought to be insane by those who could not hear the music. – Nietzsche
Be the ones who hear the music.