Sergio Díaz.

On the Importance of Vanilla Javascript

There's a very simple reason why people don't see the need to learn "vanilla" Javascript. Boilerplate code. When you are trying to create something simple and straight-forward Javascript loves getting in the way. Well, Javascript doesn't actually like getting in the way, browsers do. This gets worse when you are trying to support different browser versions. So you have to write things using the current APIs available to you and then, add a whole abstraction layer just so you can fall-back to a workable solution that might be needed to support a particular browser version which happens to be pretty old but a bunch of your users still use.

So, there's two ways to look at this, either you go with the approach that will support the most users and let the other's suffer (or degrade gracefully, ideally). Or write a bunch of boilerplate code to support browsers. Luckily, the second option is actually viable. There's a plethora of libraries other people have taken the time to write for you. These are great. They are well tested, cross-browser and are really easy to use.

The problem is that sometimes you have to add 9595 lines of codes just to do something as simple as $("code").addClass("prettyPrint"); . This is were a bunch of people will appear out of nowhere and tell you to do it manually with "vanilla" Javascript. I am not against vanilla Javascript. I actually like writing stuff in Javascript without the aid of libraries but, this is mostly unreasonable. For example, adding the class takes jQuery 38 lines. Now, you might say something along the lines that jQuery is an extreme example but, I am sure that everything that can be taken out was already taken out. So, you might just go with jQuery and call it the day, after all going the vanilla way is only feasible when you know your audience and want to tailor directly to them or when you are strict on who you support and are pro graceful degradation. Of course, if you have a time constraint it becomes even more one sided.

I personally think that soon enough, we won't need jQuery to do as much for us, with new browser APIs it gets increasingly easier to get things done with less. Things like querySelectors and CSS3 animations can go a long way when building a modern site. But, as long as we have money to be made and time to be saved and many browsers to be supported, going vanilla might not be a good option in most cases.

comments powered by Disqus