Introducing JavaScript

JavaScript has changed a great deal since it was first introduced but many sites still use outdated techniques. There are even lots of JavaScript tutorials on the web that show you how JavaScript should be written so that it can work in Netscape 2 rather than so that it will work in modern browsers. As a result there are many people who are writing what they think is JavaScript but where what they are producing is more suited to the 20th Century than to the 21st.

Beginners should work through all the basic examples prior to moving on to the other examples which cover separate aspects of JavaScript in more detail.

Most of the JavaScript examples here will work in IE5+, Opera7+ and all versions of Firefox, Safari and Chrome. Those commands introduced in ECMAScript5.1 in 2011 have been added (if I have missed any please let me know). Those commands introduced in ECMAScript2015 are currently being added (there are lots of them so it may take some time but then most current browsers do not yet support them all anyway). Where these new commands are not supported by IE8 or IE7 that information is included in the text since those two browsers might still have to many users for you to ignore. So few people using other browsers fail to keep their browser up to date that lack of support in older versions of other browsers will not have any significant affect.

There is a link to jsBin at the bottom right of all the pages. This site provides an easy way to test your JavaScript code online without needing to create an entire web page to run a simple test.

Anonymous Function

By defining all of the functions your script uses as anonymous functions you gain greater control of your functions than if you name them in the definition. You can always assign the anonymous function to a variable if you need it to be able to be referenced by name. This makes it possible to do […]

Divide By Zero

Most programming languages will throw an error if an attempt is made to divide by zero. JavaScript does not do that but instead returns one of two values depending on whether you are dividing zero into itself or into some other number. For those of you who want JavaScript to throw an error if an […]

Catching Specific Errors

The catch block doesn’t have to catch and handle all the errors that may be produced by the corresponding try block. While it will catch all the errors it is able to tell which type of error it is catching and so you can effectively only catch specific errors by testing if the error is […]

Catching Thrown Errors

If you throw an error without catching it then you are effectively crashing your own script. This may be an appropriate action where the situation resulting in the throw is never supposed to occur and would cause issues if it didn’t crash but most of the time when you throw errors you will not want […]

Error Objects

JavaScript pre defines a number of different error objects that we can use when throwing and catching errors. When throwing your own errors you should create a new error object based on the one that is pre defined that most closely matches with the type of error you are throwing.

This site is © copyright Stephen Chapman - Felgall Pty Ltd 2011-2015.

Privacy Policy | Terms and Conditions

You are welcome to use any the example JavaScript from this site in the scripts for your site or any that you develop for others but may not use the longer example scripts that contain a copyright notice in any other way without permission.