Click here to Skip to main content
13,258,055 members (51,273 online)
Click here to Skip to main content
Add your own
alternative version


2 bookmarked
Posted 28 Dec 2012

Forgiveness is a Virtue (of JavaScript)

, 28 Dec 2012
Rate this:
Please Sign up or sign in to vote.
Forgiveness and tolerance are JavaScript’s greatest virtues since the language does everything it can to prevent errors and makes erroneous code work as much as it can.

Forgiveness and tolerance are JavaScript’s greatest virtues: the language does everything it can to prevent errors and makes erroneous code work as much as it can. This is in huge contrast to ‘prima donna’ languages like C++ or Java(*1), which whine and cry at the slightest hint of error and do not hesitate at all to halt execution at the first opportunity.

Let us consider some examples. In the example below, we have a function that takes 1 argument. In the last line, I call the same function without any argument and the code works without any complaints from the JavaScript interpreter.

function foo(count) //a 1-arg function
    for (var i = 0; i < count; i++)
        console.log("count " + i);
foo(3); //call the function an argument
foo();  //***call the same function with no argument ***

The output is as follows:

objects@london(umermansoor) $ node forgivenessVirtue.js
count 0
count 1
count 2

In fact, the code above still works if we remove all semicolons (;).

function foo(count) //a 1-arg function
    for (var i = 0; i < count; i++)
        console.log("count " + i) //no semicolon

foo(3) //no semicolon here either
foo()  //double whammy. Everything works

Note: Semi-colons are optional in JavaScript and are required only if two statements occur on the same line.

Let’s look at a slightly advanced example. In JavaScript, a constructor is a function that returns a reference to an object when called with new. E.g. var object = new SomeObject().

function Console(make)    //a constructor
    console.log("The current make is: " + this.make + ". Setting make to: " + make);
    this.make = make;     //setting make property of the object

var gamingConsole = new Console("Wii-U"); //normally that's how a constructor is called

Console("PS3");           //**calling a constructor directly, no complaints.

Note: If you are curious how the second example works when the constructor is called directly and there is no context for the this keyword, the answer is that JavaScript uses the most global context in this case, normally the window object for web pages.

Why is JavaScript So Forgiving?

JavaScript is THE language of the web. It is by far the most popular language on the planet. I can even dare to bet that if you randomly pick two sites, there is a very high probability that both sites will be running JavaScript in some form or fashion.

JavaScript was designed to just work and this is the reason we don’t see sites running JavaScript crash completely in the presence of errors. Your clients don’t come running to you complaining that the site is down entirely if there are JavaScript errors. Please don’t get me wrong: JavaScript programs can still contain irrecoverable errors, for example, missing closing “)” in a for statement. The point I’m trying to make is that JavaScript will try very hard to prevent errors, but if errors are unavoidable, it will at least run the program as much as it can.

JavaScript is very easy to learn and is fun to work with. I recall my first experience with JavaScript when I was able to write a miniscule “Todo” program (or something like that) within an hour of first starting the language and the program just worked on the first try. This again is in sharp contrast with Java, which is very strict when it comes to discipline. When I started Java, for quite some time, I found it very difficult to write programs in plain text editor say vim without the assistance from an IDE say Netbeans, and run them without any syntactical errors on my first attempt.

Now back to the question: In my opinion, JavaScript had to be forgiving in order for it be to widely adopted as the de-facto standard language of the web. This freedom however was mostly not intentional. JavaScript is an outstanding Object Oriented (OO) language, however, it has more than its share of bad. A lot of different ways of doing the same thing, many different flavors, I can go on for hours talking about the bad parts of the language. In fact, there is an entire book on this subject: JavaScript: The Good Parts.


Whether the freedom that JavaScript lends to developers is a good thing or bad depends on the perspective. Beginners using JavaScript as their first language love it. Veteran developers coming from the background of disciplined language such as Java, may find themselves frustrated due to the fact that JavaScript does not force developers to behave themselves.

It seems to me that the freedom in JavaScript is well balanced on the spectrum of most expressive languages like Perl and least expressive like Java. JavaScript is an amazing OO language which is fun to work with, easy to use, and with the arrival of server side JavaScript based platforms like Node.js, it can be used as the only language (not counting HTML + CSS) to furnish complete sites. (As opposed to PHP + JS, or Python+JS).

*1: Oracle/Sun Java is an extraordinary OO language. Java and JavaScript were designed for two very different purposes, had different audiences and goals. Both are very good and popular in what they do. Context is the keyword here.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 28 Dec 2012
Article Copyright 2012 by mumera
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid