5 Common Myths About JavaScript

Tweet

Despite being the world’s most widely-used programming language, JavaScript is the most misunderstood and undervalued. The situation has improved during the past few years, but these myths still permeate the IT world.

1. JavaScript == Java

UK developer Jeremy Keith devised the best explanation:

Java is to JavaScript as ham is to hamster

The names are confusingly similar but Java and JavaScript are not related. There are a number of superficial similarities, but the same can be said for any language adopting a C-like syntax.

The language was originally named Mocha, became LiveScript, and finally JavaScript at a time when Java was heralded as the savior to life, the universe and everything. The name reflected Netscape’s marketing efforts — not any underlying relationship.

2. JavaScript is a “toy” language

The “script” part of the name gives the impression that JavaScript is some type of cut-down, macro-like or less capable version of Java. It’s not. If anything, popular JavaScript features such as closures and lambda functions are now finding their way into other languages (PHP, C# and Java itself).

JavaScript is most commonly found as a browser-based interpreted language, but that doesn’t mean it’s any less powerful or competent than an OS-based compiled language.

3. JavaScript is only found in browsers

Most people experience JavaScript as an embedded browser language but it (or languages based on the ECMAScript standard) can be found in many other places, e.g.

  • Mozilla’s Firefox browser and Thunderbird email client use JavaScript for some application processes
  • Flash ActionScript is based on JavaScript
  • PDF files can have JavaScript embedded
  • many Microsoft and Apple desktop widgets use JavaScript
  • OpenOffice.org provides JavaScript support
  • webOS, used in Palm mobiles, uses JavaScript for its application framework
  • JavaScript can be used as an application programming language on the GNOME desktop and as a scripting language in Windows.

JavaScript does not have a strong server-side presence but several dozen implementations exist. The day we’ll be able to develop JavaScript on the client and the server can’t come soon enough!

4. JavaScript is inconsistent and buggy

Those days are long gone. Browser vendors adhere to an agreed standard (ECMAScript) and JavaScript-specific issues are rare.

However, browser features are not always consistent: DOM manipulation particularities, event handling idiosyncrasies, and browser quirks all cause development headaches. That’s not a fault with the language itself, but the browser objects you can access with JavaScript.

5. JavaScript is not object-orientated

Confusion arises because JavaScript does not provide obvious class-orientated inheritance. The lack of a ‘class’ keyword causes many developers to assume JavaScript is a functional language.

JavaScript offers prototypal inheritance. The real beauty is that you can write functional language code or use classical inheritance patterns with private properties and methods.

Furthermore, everything in JavaScript is an object — including native types and functions. How many other languages offer constructs such as:


var x = new Number(123);
var opp = "oops".substr(0,3);
var f = function() { return function() { alert("hello!"); }; };

What about passing a function as a parameter (it’s just another object)…


var Add = function(a, b) { return a + b; };

function Calculate(func, a, b) {
	return func(a, b);
}

var s = Calculate(Add, 1, 2); // s = 3

Or perhaps extending native types…


// reverse any string
String.prototype.Reverse = function() {
	return this.split("").reverse().join("");
};

var h1 = "Hello!";
var h2 = h1.Reverse(); // !olleH

JavaScript can be a little confusing at first but you’ll miss its elegance and power when you return to other languages.

Still don’t believe me? Did you sign up for Kevin Yank’s JavaScript Live course? I’ll see you in the forums.

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • Chris

    Great post!
    Only one correction, on point 5 “…The lack of a ‘class’ keyword causes many developers to assume JavaScript is a functional language…”
    The correct phrase would be “…The lack of a ‘class’ keyword causes many developers to assume JavaScript is an IMPERATIVE language…”
    Imperative means old non OO languages like C or TurboPascal.
    Functional languages are completely another world. Google about Haskell or the new .NET’s F#
    Cheers!

    • Szymon Jeż

      JavaScript IS an imperative language. It’s an imperative language drawing on object-oriented concepts (in this way it’s like like C++, Perl, Java, PHP, Ruby). I suggest You to check what does it mean – http://en.wikipedia.org/wiki/Imperative_programming.
      You are right that using “functional” in “…The lack of a ‘class’ keyword causes many developers to assume JavaScript is a functional language…” is not correct. The best world would be, in my opinion, “non object oriented”, “not capable of using object oriented programming techniques” or “procedural”.

  • NetNerd85

    Great, now do one for Flash!

  • http://www.optimalworks.net/ Craig Buckler.

    @NetNerd85
    That’s easy – simply negate any Flash comments made by Apple!

  • commandnotapple

    @Craig

    What is that supposed to mean? I don’t hate on Flash just because it seems to have become a fad of late, but it is a nasty beast and it’s derided for a reason.

  • dusoft

    Javascript can be also used to develop Plasma widgets for KDE desktop.

  • http://www.optimalworks.net/ Craig Buckler

    @commandnotapple
    Apple has been (somewhat unfairly) Flash-bashing recently. But we’re going off-topic a little … there are many other Apple vs Adobe debates raging across SitePoint.

    • http://www.inhouselogic.com CousinsJM

      I take your point Craig. But regardless of the cause, the practical upshot was a high level of frustration to novices and experienced users alike.

      The recent popularity of libraries like jQuery, which explicitly account for the differences in the DOM interpretation, speaks to that issue.

      • http://www.optimalworks.net/ Craig Buckler

        Absolutely. jQuery is popular because it takes the pain out of DOM manipulation and gives developers quick and easy effects to play with.

        Also, the great thing about JavaScript is that you can write code which adds or fixes missing bits, like String.Trim() or document.getElementByClassName().

  • Java/Java Script

    Java/Java Script

    Yes I’m glade to know that someone else understands just how good Java Script can be.

    http://www.acomputerportal.com/java_and_java_script.html

    I’ll add this information link to my Java/Java Script page above.

    • picky

      JavaScript one word.

  • CousinsJM

    Might take a poke at #4. It’s not really a myth if it was true not that long ago. And, ho-boy, was it ever true! It frankly kept a lot of us “old timers” from bothering with it until recently. Still true in some ways, as this article notes. May not be the fault of the language but the practical issue remains.

  • http://www.optimalworks.net/ Craig Buckler

    @CousinsJM
    I’m not convinced JavaScript was ever particularly inconsistent and buggy. Microsoft even reverse-engineered all the bugs so JScript matched NetScape’s implementation.

    But I think it got a bad rap because the browsers and DOM manipulation methods themselves were nasty. And few people appreciated the language’s power (I didn’t); it seemed backward when everyone was using classical OOP on the server.

  • FatMike

    After reading this post I have realized you like Mr. Douglas Crockford!
    Me too :)

  • Rene

    @Cris: OOP is just a style of imperative programming. It would be better to say: developers assumed JavaScript is a procedural language.

  • Matthew Frederick

    On Javascript on the server, it was once, momentarily, huge. Netscape’s early server product LiveWire was all Javascript on the server, and lots of major websites were built with it. Microsoft came out with Active Server Pages as a competitor, and it was only a matter of a few years before LiveWire mostly vanished.

    Still, I loved the days when I could write Javascript on the server and Javascript on the browser. Great fun.

  • http://www.yellowshoe.com.au/ markbrown4

    JavaScript is not object-oriented. I think it’s best not to confuse the issue.

    • http://www.optimalworks.net/ Craig Buckler

      Why do you think that?

      • Sam Vloeberghs

        I would like to hear that answer to :D

      • V1

        A better description would be..

        “Javascript class-free, object-oriented language, and as such, it uses prototypal inheritance instead of classical inheritance”

        As Mr Crockford puts it :

        http://www.crockford.com/javascript/inheritance.html

      • Khoa

        Can you do inheritance and polymorphism with JS? If not, I dont think it is fully OOP though.

  • Marin Todorov

    Yup, Javascript is probably the most controversial part of the web. It has its huge drawbacks, but also great advantages. A good round up I’d say, it’s always good to bust some myths

  • stetro

    JS is object-based not object-orientated, isn’t it?

    stetro.wordpress.com

  • Xanti SS

    Wow, first JavaScript post I’ve seen on a pro design/dev blog for a while that doesn’t mention jQuery. THANK YOU.

  • eagles11

    Great article . I wish I had something more insightful to see but you have pretty much covered it all here. ThanX

  • Anonymous

    >> Furthermore, everything in JavaScript is an object
    This is not entirely true. In fact it is not true at all.
    var s = “string”;
    s is not an object. Despite you can write alert(s.length);

    • Abba Bryant

      @vbulliten: you are so wrong.
      String’s are primitive Object’s in javascript.

      You can instantiate a new string with the new keyword. IE it is a class.

      var s = “string1″;

      var t = new String;
      t = “string2″;

      alert( typeof( s ));
      alert( typeof( t ));

      • Anonymous

        If you are so smart, explain this code:

        var a = “string”;
        a.name = 3;
        alert(a.name);

        Why it will alert undefined if a is object?

    • Erik

      String is an object. You can use the prototypal inheritance chain – and add the method for the instance of the String object :

      s.constructor.prototype.name = 33;
      s.name ; // 33

  • http://www.optimalworks.net/ Craig Buckler

    @Khoa

    Can you do inheritance and polymorphism with JS?

    Absolutely. It’s not written in the same way as other languages, but that doesn’t mean you can’t do it. Google it if you’re not convinced!