Zen of JavaScript

A long time has passed since JavaScript became mainstream. And despite multiple attempts, there is still no right single way to write code in this language. Everybody is using it in their own special way and most people still fill that they are failing to tame the language.

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. 😉

Prepare to meet JavaScript… again.

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.

Dmitry Baranovskiy – Zen of JavaScript

Dmitry is not about to tell us that everything you know about JavaScript is wrong… although it is…

Dmitry remembers when he first met JavaScript nobody called it JavaScript. When he was studying at university, he was told “you can use any language you want” he chose JavaScript. It was 1997. People were laughing because it was “not serious”.

The problem with JavaScript is everybody knows it, nobody knows it. People don’t pick up JavaScript so much as run into it.

JavaScript has long been considered a bit of a joke. People have bent JavaScript to meet what they expect – you can tell JavaScript written by a Ruby guy or a Java guy. Even JavaScript developers layer on Angular and React and that changes the way you write.

But what is the JavaScript way of writing JavaScript?

There is no wrong way. JavaScript was written so that any way is the right way. “You can still write shitty software, don’t worry…”

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!

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.

Equality.

Is JavaScript good or bad at equality? Depends who you ask and how you look at 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.

People look at JavaScript and come up with theories how it works, then they code according to those theories. Don’t make up stupid theories – read the spec. There’s no mystery. It’s documented.

Acceptance.

JavaScript is very accepting. It accepts all your hate, all your blame. All your love.

.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”

Emptiness.

Emptiness is very important in Zen. JavaScript has three emptinesses: NaN null undefined

Undefined isn’t even null. It’s more empty than null.

But JavaScript has something even more empty than that… JavaScript can have a placeholder that returns undefined. But if there’s no placeholder, it’s not even undefined.

There is nothing new.

The “new” keyword doesn’t smell like JavaScript. It was put in to smell like Java, which was fashionable at the time. As Java stopped being fashionable, people started leaving it out.

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().

Date() is an alien function, not from JavaScript, it was brought in from Java.

JavaScript and exceptions are things from opposite universes.

42/0 in JavaScript doesn’t throw an exception. It returns Infinity.

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.

Forgiveness.

People write rubbish. JavaScript forgives you.

All your bad code. All your libraries. You want to run JavaScript on servers, on phones. JavaScript forgives you.

You want to put commas first? You want to omit semicolons? JavaScript forgives you. Dmitry doesn’t. But JavaScript does.

Is it a good thing, or a bad thing? Dmitry doesn’t know. But it’s how the language is written.

JavaScript is not a weak language, it’s a flexible language. It’s an encouraging language. You don’t have to follow the rules of Python or Java to write JavaScript, but you can. You can follow your own rules if you want to. It gives you that freedom.

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.