10 reasons why Dart is cooler than JavaScript


Dart is a new programming language from Google and after coding JavaScript for over one year now, I immediately felt in love with it. Coming from the Java world I had a good bunch of things I had to learn before I could use JavaScript.

Some people say, you need to dig deep into JavaScript, otherwise you are not allowed to speak about the pros and cons of a language. I am not a JavaScript Ninja. But I strongly believe a programming language should be easy to learn, easy to understand and should be consistent in terms of language constructs. Even after all the long time I dealt with JavaScripts weakness, and even when I meanwhile think JavaScript has some pretty cool ideas, I must say that I need to be very, very, very carefully every day when I work with it. And that’s bad. A language must support his programmer and not vice versa.

Here are some of the JavaScript flaws which will make me switch to Dart, once it is ready for production. Please keep in mind that Dart is in the makings.

1. Dart uses only one falsify

Looking at this post, you will easily see the point. The values: false, null, undefined, “”, 0, NaN will evaluate to false. Means you can write things like this:

var a = null;
if(!a) {
   // do

In Dart there is only one thing which is false. It is false itself. Of course you need to rewrite to this:

var a = null;
if(a != null) {
   // do

Using 6 falsifying expressions is well, let’s say you need to learn about it.

2. Dart can work with types, if you want it

JavaScript devs often say types kill flexibility. OK, might be true. But too much flexibility will kill your software. Sometimes you want to work with types, and with Dart you can do it - if you want. Just enable the type checker and go ahead.

3. You need a framework to work proper with DOM

In JavaScript you can work with these old friend:


Isn’t it nice? Thanks heaven we have got jQuery (and friends) to help us out of this. But… should it really be necessary to use a framework when simply selecting from the DOM? Not today.

Dart has looked at jQuery and has stripped it down to 2 methods:


Much better.

4. Classes and Interfaces

When Java developers start with JavaScript they often try to write JavaScript like they write Java-Code. There are constructors and class like elements. Of course this is not the way JavaScript should be programmed. It is prototype based, everything is an object. This is somehow cool. But you can throw away many of your Gang of Four Design Patterns and instead read about JavaScript design patterns.

Honestly, some of the developers I have worked with have invested a long time to understand the “mainstream” patterns. Not everybody is a geek. Using a non-mainstream programmin language in a mainstream environment leads to the center of ultimate chaos.

5. Inheritance

Dr. Rauschmayer explained at his excellent blog post why JavaScript inheritance is easy. He is right. It is easy. But be warned: his recommendation is not the only way for JavaSCript inheritance. The Frameworks Prototype and jQuery have even created “extend” Methods. Instead of Dr. Rauschmayers use of proto you can use the prototype keyword (and yes this guy even speaks of classes, even when there are no classes). Of course you can implement your own extension mechanism and simply copy every property yourself.

All the results popped up when searching Google for “javascript object extends”. It is a confusing mass of different approaches for achieving a simple goal: extensions.

Guess not everybody reads Dr. Rauschmayers blog. It would be so easy if.

Dart does know classes and therefore does now the “extends” keyword. Pretty damn simple.

6. Global namespace

In JavaScript you need to take care that you don’t put stuff in your global namespace. Honestly, this can be done so easy. If you miss a “this” or a “var” you have a variable on a global level. Every script has then access to it. This is terrible. Try to not mess up things. Thanks to Stoyan Stefanovs Book JavaScript Patterns I have learned a pattern to keep my namespace clean. I feel better now and have more control over what I do. But: I need a pattern for it. This is such a basic need and does not come out of the box.

In Dart you develop in “library” scope. Means, you have a keyword “library” and only what is public is visible outside of it. In addition, every Dart script executes as its own Isolate. Means, it has a fresh state, without the mess of other scripts around. With Dart you should still think about visibility and libraries, but its way easier and for sure you don’t need a book teaching it to you. Instead, just think about “Separation of concerns” and you are in the game.

7. Dart knows concurrency

With JavaScript things are not really concurrent. Even when you make an “asynchronous” request from jQuery, you are still working with one “thread” only. I think there is some chance to get more from V8, but I am not sure on it. You can solve it with HTML5 and webworkers.

Dart knows Isolates. These are little beasts similar like in Erlang. They can communicate to each other. If one fails, another Isolate can simply restart it again. Isn’t that cool? And of course this makes Dart very nice for server side programming too. Yes, I have heard of Node.js. But that is not my point. Dart can do that magic out of the box.

8. JavaScript doesn’t know foreach

Oh wait.

You can extend Object or the Array.prototype which is pretty nice.

Or you do the following for arrays:

for (var i = 0; i < elements.length; i++) {
  // do something

And you can even do that for objects:

for (key in elements) {

But unfortunately Douglas Crockford (a very important guy in the JavaScript world) recommends not to use this statement. Reason: your results are not ordered and you might members from the prototype chain or function names.

Of course you can filter with hasOwnProperty.

Finally you should look into your frameworks docs if they offer something like forEach. jQuery does.

In Dart:

for (element in elements) {
  // do something

Life can be so easy. You iterate here over a list of elements.

9. Weirdness intializing arrays

This one is borrowed. Look at this:

var a1 = new Array(1,2,3,4,5);
var a2 = new Array(5);

a1 is an array with 5 elements: [1,2,3,4,5] a2 is an array with 5 elements to: [undefined,undefined,undefined,undefined,undefined]

Dart is much more cleaner. An array is basically a List and therefore has this interface.

List a1 = [1,2,3,4,5];
List a2 = new List(5);

Again, a1 contains 5 different elements. a2 contains space for 5 elements. In addition you get nice features like “removeRange” or support for sorting. Check out Seths blog.

10. undefined and null

There is much you need to learn when starting with JavaScript. Among them is the holy knowledge that there is not only null, there is undefined too. It is a type with only one value: undefined. It can be overridden. And you can get it by various scenarios, for example if you call return but don’t return any value. At the linked page you can even see how you can deal with a possible overwritten undefined value.

It seems null can be replaced by undefined in pretty much scenarios.

This is sick.

Dart does only know one null.


JavaScript has some good parts, of course. There are some nice patterns. But for the moment, I have seen nothing I can’t do with Dart. Dart is mostly more elegant and easier to read (to my taste). Some hardcore JavaScript devs will think different, but that’s ok. You need to love JavaScript if you want to work with it (or arrange. Dart on the other hand is pretty mainstream. And that’s good.

PS: I missed the event handlers in this list - another thing which is pretty much cooler than in JavaScript. Check them out.

Tags: #Dart #JavaScript