DZone Top Articles of 2011: Why Java Developers Hate JavaScript
This is the first entry in DZone's series that will revisit the top articles of 2011 throughout the month of January 2012.
As a Java developer, I used to hate JavaScript. At one time I used to say “friends don’t let friends use JavaScript”.
In recent years, JavaScript has become required for a flexible & feature rich user experience. WIth the advent of tools such as JQuery and GWT many of these issues have been minimized. Also, I took the time to read more about JavaScript. The book that really opened my eyes is JavaScript: The Good Parts.
So… What do Java developers hate?
- JavaScript may look like Java, smell like Java and even taste a little like Java, but it does not behave like Java
- Variables are global in scope
- “this” has different meaning based on the invocation type (learn this for your own sake)
- Braces do NOT guarantee scope
- Functions are first class citizens, not classes (avoid classes, you will set yourself up for disappointment)
- Functions are objects, not simply methods to a class
- Variables are dynamically typed objects
- JavaScript often fails silently (this is for user experience)
- Difficult to debug (use FireFox & Firebug addon)
- Semicolons are optional (bad idea, put them where you want them)
- Functions always return values, a value or “undefined”
- “new” has some specific assumptions, not simply creating an Object
- Cross browser compatibility (use jQuery or GWT to address most issues)
What should you understand about JavaScript?
- Functions – First class citizens, the primary object in JavaScript
- prototype system – Object.prototoype, object linkage, delegation, hasOwnProperty() method, etc.
- closures
- currying
- the 4 invocation types: method, function, constructor & apply
- dynamic languages such as groovy, ruby, etc. they share many similarities
What should you forget when using JavaScript?
- Classes
- Scope using braces
- Strong typing
In summary, it is an entirely different language with many syntactical similarities. JavaScript is very power and flexible once you understand how it behaves. I highly recommend the O’Reilly book (mentioned above), its a short read and packed with awesome facts.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)





Comments
Javin Paul replied on Mon, 2011/01/17 - 7:53am
Hi,
I completely agree your point "JavaScript may look like Java, smell like Java and even taste a little like Java, but it does not behave like Java" . Java is such a gem languare and it gives you pleasure of coding while Javascript is give feels more of scripting than programming. anyway both are important in there place but I must say Java is Java :)
Thanks
Javin
Arnaud Des_vosges replied on Mon, 2011/01/17 - 8:33am
Yes, for me, the main problem of Javascript is the lost of "static" typing compared to Java.
(Maybe "static" is a better term than "strong" here.)
I would switch to GWT just to get that.
We must add many comments in the JS source code only to describe argument types.
At least if modern JS runtimes could include optional typing, it would be nice.
See http://www.mascaraengine.com
Christopher All... replied on Mon, 2011/01/17 - 9:15am
Maybe it's because I come from the opposite direction (I've spent more time dealing with ECMAScript derivatives than Java) but I actually have to disagree. JavaScript does not really have the look, smell, or flavor of Java at all.
In Java, an array is generally an efficient means of storing data of a particular type and HashMaps are more than just String lookups. Not so much with JavaScript. In JavaScript, variables (which are generally bloated, unseemly things, to be honest) are made local with the "var" keyword while there is genuine scoping in other languages.
On the other hand, JavaScript allows you to return a function *as a value*. This is major (and has been since long before SICP was written). Java... not so much. If you want an anonymous listener in Java you have to jump through some major hoops. With JavaScript, length is a property. No matter what you are counting (array and XML), it will always be a property. In different situations in Java sometimes it is a property, sometimes it is a method, sometimes it is undefined.
If any of the ECMAScripts are like Java it is ActionScript 3... sort of. But even there, it is missing true abstract classes (you can force it, but that is annoying). There is no way to have a protected or private constructor (meaning Singletons are hacks). And, in my opinion the worst part, is that there is no overloading (and that is true for all ECMAScript variants). For that matter, optional variables in AS2 and JS are both hacks. There is an implementation in AS3 which is adequate, but even there it isn't nearly as nice as overloaded methods in Java (though I think that C# has the best as far as I've seen).
Mike Pavlasek replied on Mon, 2011/01/17 - 10:28am
Karl Peterbauer replied on Mon, 2011/01/17 - 10:32am
Olli Plough replied on Mon, 2011/01/17 - 10:55am
Manuel Jordan replied on Mon, 2011/01/17 - 12:01pm
in response to: coplas
Thats correct, I am agree totally with you, anyway about this, exists other option than jQuery?
Manuel Jordan replied on Mon, 2011/01/17 - 12:07pm
Good article
BTW about this Functions are first class citizens, not classes (avoid classes, you will set yourself up for disappointment) is really a fantasy this about classes, totally agree with you
Exists other problem not consider here, the code is totally visible writing the correct URL, therefore an attack/hacker can know some logic in validations even if we work with Regex
Your experience is valuable, some way to handle this problem?
Mauricio Garavaglia replied on Mon, 2011/01/17 - 2:35pm
Ahmed Shareef replied on Mon, 2011/01/17 - 6:13pm
Report Scam
josef betancourt replied on Mon, 2011/01/17 - 7:15pm
I don't "hate" JavaScript. In fact, it is a great language and is driving much of the modern world via the browser.
If you appreciate the power of Javascript, as detailed in your post and others, Java also has many dynamic and scripting languages that run in the JVM. I particularly like Groovy, but look to also dabble in Scala. Thus, these languages allow you to use the 'safe' business language and yet also start using more powerful language features and idioms. At least, until Java itself starts to implement them.
Cosmin Mutu replied on Tue, 2011/01/18 - 2:17am
Why Java Developers hate javascript ... do you have a survey on this?
Javascript is nothing like java ... but I don`t hate it. Heck, I`ve used it many many times and with success too :)
@Manuel Jordan - Since javascript is run on client side (by browser) it is normal to be easily accesible. If you wish to protect your javascript code, obfuscate your code, that`s the best you can do (or at least to my knowledge).
Gerard COLLIN replied on Tue, 2011/01/18 - 4:31am
in response to: metalj
Gerard COLLIN replied on Tue, 2011/01/18 - 4:32am
Jan Kotek replied on Tue, 2011/01/18 - 4:36am
Albertas Laurin... replied on Tue, 2011/01/18 - 6:38am
Philippe Lhoste replied on Wed, 2011/01/19 - 10:05am
"Javascript is nothing like java"
Well, its syntax have been designed to look like Java, with braces, semi-colons, similar for loop, and so on. It was a ploy, like the name, to seduce Java programmers (Java was the hype when JS was created).
Somehow, it is part of the problem: on one hand, people like to find a familar syntax. On the other hand, a different syntax is a reminder of different concepts: when you code in Python or Lua, you won't forget it isn't Java, the syntax helps in setting the correct mindset. Similar syntax can bring confusion if you are not concentrated, particularly if your application mixes both languages.
"Why Java Developers hate javascript ... do you have a survey on this?"
Good remark, although my personal experience with co-workers is similar: they wouldn't touch JS with a ten feet pole, that's why they chose GWT to do Web-based applications...
Beside the fact that's another language to learn, they complain about lack of good IDE support (compilation, auto-completion, debugging - they don't know Firebug). Indeed, that's a weak point with dynamic languages.
Personally, I coded in JavaScript before I learned Java, so I have nothing against the former. I appreciate both, and enjoy the different paradigms.
Andrew Spencer replied on Wed, 2011/01/19 - 10:54am
Nikunj Bhatt replied on Wed, 2011/01/19 - 5:06pm
Just the names are similar but their actual uses are very much different and therefore they must not be compared. Both languages have their own elegance of syntax and use.
@Gerard COLLIN
You have misunderstood the content which is provided in the link provided by you and the content is also not much descriptive. JavaScript isn't a baby of Java, because JS was made for totally different purpose and Sun Microsystem was not involved when the LiveScript (the first version of JavaScript) was developed by Netscape. After almost 3 months of LiveScript, Netscape asked to Sun to give the name JavaScript to gain advantage of Java's popularity. [please take a look at here : http://en.wikipedia.org/wiki/Javascript , search for "Sun" on the page and read the 2 lines before and after the first occurrence of "Sun"]
I love enjoying JS's flexibility.
JS has more brighter future than Java because future is of web and Java is now not owned by Sun, Oracle has overtook Sun and Oracle is not much focusing on Java how much Sun was (according to some news I have heard from my co-workers, Java developers).
Raveman Ravemanus replied on Mon, 2011/01/24 - 4:12am
in response to: karl
Liezel Jane Jandayan replied on Mon, 2011/08/22 - 9:10pm
Erwin Mueller replied on Tue, 2012/01/03 - 2:44pm
I don't like ECMAScripts because it is the only choice for web sites. I don't like to have something carved for me in stone, because it's never the right answer for everything.
There is no reason why the standard should demand a language for the DOM manipulation. All the major browsers already compile JavaScript to byte code with a VM to make it run faster. Why there is no option to use different languages that are compiled to the same byte code (like it's done in Java with the JVM). The standard should mandate the API, not the implementation (aka JavaScript).
Also, I don't like ECMAScripts because it's over hyped as a rich client solution, and it makes the development of the web go in a wrong direction. The development of the web should go into delivering information to the user and not in developing games and rich clients.
The web should make information easily accessible, enriched with semantics, so that we can consume the information more easily.
This rich-text form for input the text is a prime example of what is wrong with ECMAScripts and the development of the web. Instead that it's just a simple tag like <richtextinput>, it's a some kind of frankenstein monster of JavaScript hacked together.
I hate this rich-text form in DZone. Not only it does not paste text correctly, it will delete 2 characters sometimes if I hit the backspace key. Also, the formatting is horrible implemented.
Why there is no <richtextinput> tag so the browser can show me a nice native text input panel, that is consistent over all web pages? Why do I have to see on every web site a different rich-text text-input panel, only because everyone needs to hack together more of the frankenstein monsters of JavaScript.
That is what is wrong with JavaScript. A web site is not a rich client platform. Deliver me information and stop hack together some stupid.
Mohamed El-beltagy replied on Wed, 2012/01/04 - 1:41am
I'm a Java developer who used to hate JavaScript couple years ago.
It used to mean for me two things: 1- A cause for deep pain and frustration. 2- A necessary evil for client side validation.
Although second part still valid (due to the project's nature I involve in); the first part has changed deeply since I learnt lots of OO Development in JavaScript.
My advice for anyone who does not JavaScript OO development is to read and test lots of it. It will clear lots of issues in your mind and set lots of must have concepts and basics in JavaScript development.
Well, other thing that helped me understanding JS are the modern browsers. I used to debug JS using FireFox and FireBug. And then I learnt using IE's (version 8) "Developer tools - F12".
By the way, at that time I have not used JQuery or any other library.
Currently I use and prefer Dojo.