JavaScript - is the new Basic!
Written by Ian Elliot   
Friday, 30 September 2011

We noticed the rise of the new star some months ago. Now it's no longer possible to ignore the simple fact that it is everywhere. Yes - JavaScript is the new Basic!



We noticed the rise of the new star some months ago. Now it's no longer possible to ignore the simple fact that it is everywhere.

Yes - JavaScript is the new Basic!


How you feel about this statement depends very much on how you used to feel about classic Basic and on how you currently feel about JavaScript. It is a sentence that really throws into high relief the current situation that JavaScript finds itself in.

Basic was the first choice for the beginner and, after they had put they work in to learning it, they used it. JavaScript is about to take on the same role of being the universal language.

JavaScript was a poor and humble scripting language until it had the mantle of greatness thrust upon it. Now it is the single most pervasive computer language in the world. You can't rate its success by the TIOBE index or the number of questions asked on Stack Overflow because this isn't just a professional programmer's language. It is the people's language, just like Basic once was.

JavaScript turns up in most web pages and in big projects. If you are a professional programmer don't worry, JavaScript is making its way to a project very close to you very soon. JavaScript is not only the way to build web apps on the web and phone apps on phones and mobile devices, but now it is a central language in Microsoft's application environment.

What language is central to Windows 8? Why JavaScript of course.

Learning and teaching JavaScript

As we reported recently, JavaScript is being used to teach computer science and yes, why not use it as a beginner's language? There can be no more useful language to learn after all and it simpler than the traditional object-oriented languages because it isn't class- or type-based while still being object-based. It is the once and future king making its way into every area of computing and it is time to look at how we regard it and what it "feels" like as a language.

The good news for teaching programming, and the bad news for actually doing programming, is that JavaScript can be used as a purely procedural language. That is, you don't have to know anything about objects to write a "Hello World". More to the point, you can write some fairly huge programs without out ever creating an object or using objects to organize the code. You could use this fact as a stick to beat JavaScript with, but notice it is a choice - if you want to be bad you can be but if you want to be good you can learn about and use objects.

You can also see this is a huge advantage. Have you ever seen the way a beginner's eyes glaze over when you start to tell them about objects before they have even written a single line of code? Being able to show how simple algorithms translate to code is a really big advantage and gets them off the launch pad very quickly. Once they are approaching orbit you can start to talk about objects and methods - and they already know about methods as procedural code. What is more after a few medium-sized procedural programs they are usually more than happy to find that there is more to life than one long main program.

So JavaScript is object-oriented but not aggressively so.

There are a number of other really important things about JavaScript that programmers coming from other language find hard to swallow - but beginners seem to appreciate. The first is that, as far as possible JavaScript doesn't use the idea of data type. Sometimes it has to for fundamental data types, but even here its type juggling rules automatically convert between numbers and strings. What this means is that you can avoid one of the most difficult things to explain to a beginner - why can't a number be displayed in a text box. In JavaScript you can just write:

 alert("some data" 1); 

and it all works. No explaining that "1" isn't the same as 1, which is something beginners just can't follow when they first start. Later it becomes very understandable and you can explain the magic that JavaScript is doing converting from strings to numbers and so on.

The second, more upmarket ,difference is that JavaScript may be object-oriented, but it isn't class-based. That is, you don't first define a class and then create objects using it. You simply create an object as and when you need it.

This is perhaps the hardest thing for a traditional object-oriented programmer to come to terms with and most spend a lot of time trying to reinvent class and all of the mechanisms that go with it - like inheritance and a type hierarchy. A classless language doesn't need any of this and a beginner can quite happily just build objects.

If you are teaching a beginner about programming using JavaScript my advice is don't mention class, don't mention type and don't mention inheritance.

The prescription is to just get on with building objects.

An object either has the method and/or property you need or it doesn't. Objects are defined by their collection of properties and this can be changed dynamically. If an object doesn't have a property you can add it or conversely take it away. Objects are dynamic in a very free way. You can think of this as a hierarchy if it suits a particular application but you don't have to if you don't want to. The big problem is that class-based languages spend a lot of effort on building and managing type hierarchies induced by inheritance rules. Similarly class based programmers spend a lot of time working around these hierarchies when it turns out they don't fit. A lot of time can be wasted on pushing a project into a mould that it doesn't fit. You may well want to see a car as "descended" from a more basic vehicle class but you can just as well describe a cars as motorbike with four wheels. Neither description captures the relationship as adequately as the list of properties that they have - some of which are common to both.  Type hierarchies force tree structures on general graphs.

Of course, there are slightly more complicated bits of JavaScript just below the surface. You may well be able to create an object but what if you want more than one of the same type? The solution to this problem brings us to JavaScript's great plus point - functions are objects.

This is an amazing trick and it is where the world of modular top-down programming meets all sorts of advanced ideas. As functions are "first class" objects you don't need delegates or function pointers because functions can be passed around just as easily as any other object. Functions can also have properties and methods of their own - and this is perhaps the most mind blowing idea to any programmer schooled in traditional class-based languages.

Yes function can be a complete object that has methods and properties and as such you can use a function to create other object - as a class factory or as a constructor depending on how you want to do things. A constructor has the extra facility of a range of properties that relate to the objects constructed including a prototype chain which can be used to improve the efficiency of object implementation. Notice the prototype chain isn't about inheritance it is about efficiency and the only time you need to use it is if you are planning to create lots of instances of a class. You really can avoid talking about inheritance and all that.

Digging just a bit deeper and you find that JavaScript is great for data structures. The basic data structure is the associative array and this lets you build link lists, trees, queues, deques - almost anything you like and without a pointer in sight. Of course if you want to invent a pointer object you can - but they really aren't needed. Recursion? No problem.

So JavaScript is one neat little language and if treated properly an ideal vehicle to teach a complete beginner and even advanced computer science.

So how does this make it like Basic?

Because as well as being a language based on principles it isn't afraid to do things different if it makes it easier to use. JavaScript is a street language that just happens to have a pedigree.

Ok - it does have some faults. But it is important to realize that many of its so called faults are just the result of a programmer trying to make it look like something else. You can't complain the it doesn't have strong typing because this is because it doesn't have classes and this is by design not an oversight.

JavaScript does have some real problems but these are mostly connected to the environment it is usually found in i.e. HTML and the DOM. Objects in the DOM behave differently - they are not pure JavaScript object but they can be mostly treated in the same way.  Many of the problems also relate to restriction due to security concerns and due to variation in the way browsers implement things. JavaScript is cursed by not having a uniform environment in which to run.

One problem is that there isn't a well supported teaching environment for JavaScript (write and let me know if you disagree but notice the term "well supported"). You can use any of the standard IDE - Eclipse, Aptana, NetBeans and so on but they all bring far more than you need to being learning JavaScript. In fact one downside to using JavaScript as a teaching language is the fact that you have to bring HTML in at an early stage if you want to get into exciting things like graphics - Canvas, 3D graphics - WebGL and the UI in general.

The more you think about it, the more it makes perfect sense to start with JavaScript and carry on with JavaScript. It really is the new Basic.

Further reading

JavaScript used to teach computing

JavaScript inherits the earth



JavaScript Jems - The Inheritance Tax

JavaScript should not be judged as if it was a poor version of the other popular languages - it isn't a Java or a C++ clone. It does things its own way.  In particular, it doesn't do inheritance  [ ... ]

JavaScript Jems - Objects Are Anonymous Singletons

JavaScript should not be judged as if it was a poor version of the other popular languages - it isn't a Java or a C++ clone. It does things its own way.  In particular, every object can be regard [ ... ]

Other Articles

Last Updated ( Friday, 30 September 2011 )