You Know You Should Use JavaScript When…

Avatar of Chris Coyier
Chris Coyier on

You know you should use JavaScript when the task cannot be accomplished with any other technology.

That’s what Doug suggested when I asked him for a nicer spin on a something that was going through my head:

JavaScript: If it can be done in another language, it should be done in another language.

That just sounds so negative, which is definitely not what I’m getting at here. But I am trying to make a point. JavaScript is capable of doing taks that can also be accomplished in other languages. JavaScript is also capable of doing things that cannot be done in any other language. Some examples are probably in order.

Form Validation

You can validate a form with JavaScript. You can create a great user experience doing it this way, but of coures you’d be dumb not to validate all the inputs again with a server side language before processing them. If you are going to pick one or the other, go server side. If it can be done with another language, use the other language.

Inserting Content

I just had a circumstance where I needed to load in a block of dynamic navigation onto the homepage of a site that was created by a CMS in a subdirectory of the site. Ideally, this navigation would be created in a file that could be server-side-included on the homepage and anywhere else that needed it. Long story short, I couldn’t make it happen. Instead, I used jQuery load function to grab the block. Works great, unless you don’t/can’t use JavaScript. If it can be done with another language, use the other language.

In a similar vein, the whole concept of AJAX is generally regarded as a hack. It is a way to simulate two-way communication in a technology built for one-way delivery. When something new comes along that more naturally accommodates two way browser-server communication, AJAX is gone. If it can be done with another language, use the other language.

Styling

JavaScript is capable of applying styling to elements on the page. That’s also the job of CSS. Having a bunch of styling information in the JavaScript muddies the water. It makes the JavaScript harder to read, and styling updates more difficult to track down. If it can be done with another language, use the other language.

One of the major reasons JavaScript libraries are popular is because they open up some awesome design possibilities. You can fade things in and out, animate sizes and positions, and do it with smoothness and style. But these things are design related, not (usually) functionality related. Hence, we are seeing CSS starting to accommodate doing these same exact things. I would argue CSS is the appropriate place for this, rather than the somewhat hacky “rapidly altering the DOM” technique JavaScript libraries use. If it can be done with another language, use the other language.

When TO Use

Alright enough don’t-use cases. There are many more circumstances where using JavaScript is the only option. How about events? JavaScript is the only language available for having your website communicate with the browser and watch for events: clicks, double clicks, mouse enters, mouse exits, key presses, browser window sizing… the list goes on. If you need access to those events, you are in JavaScript territory.

Am I right? Is this narrow minded thinking?