1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

JScript is not Javascript

Join the Discussion

Questions? Comments?

This is something that is not obvious to many people because the languages are very similar and code that you write to run as one will often run quite happily as the other. Occasionally though, one of the differences between these two languages will make a significant difference in how the code works.

One such difference recently caught me out with the code that I had written for the analog clock script.The Javascript code that I wrote included the following declarations:

var am = 'AM';
var pm = 'PM';
var ampm = '';

The idea was to assign either the am or pm variable to the ampm variable depending on the time of day. This value was then put into a field on the web page using the following code:

document.getElementById('ampm').innerHTML = ampm;

This works fine in Javascript but JScript gives an error on the statement that assigns the value of am or to the ampm variable.

The problem is that JScript treats references to ampm and document.getElementById('ampm') as both referencing the same object where Javascript does not allow you to refer to parts of the document directly like that and so recognises the ampm variable and the object within the page having id="ampm" as two entirely separate fields.

The solution of course in order to get the code to run both as Javascript and as JScript is to make sure that you don't give your variables names that are also used as ids within the HTML. Changing the ampm variable name to something else fixed my code allowing the script to work correctly as both languages.

Most web browsers with the exception of Internet Explorer understand Javascript and so the original unmodified code worked properly in all of those browsers. Internet Explorer doesn't understand Javascript. Instead it treats any Javascript as if it were Microsoft's proprietary language JScript and processes it accordingly thus giving errors in IE when you use the same name for a variable as has been assigned within the web page source.

It just goes to show that even when you make a trivial change to your script that you know will have absolutely no effect when the script is processed as Javascript that you will still need to retest the script on IE in order to ensure that you haven't fallen foul of one of the many annoying "features" of JScript.

©2015 About.com. All rights reserved.