An introduction to JavaScript

An introduction to JavaScript, how it was born, what is it good for, different types and versions of this scripting language, and the compatibility between it and the browsers.

This is a fragment of a book that I started to write some time ago, named ‘JavaScript class’. The lack of time made me stop writing at this book. Here are the first pages of the book.

JavaScript history

JavaScript and Java

It all started with Java. Java, developed by Sun Microsystems was intended to be used only in consumer electronics, but now it’s used as an advanced programming language for the Internet. First browser to implement Java was HotJava, developed by Sun Microsystems for testing purposes. Netscape integrated it in its browser, Navigator, and after this Java gained interest.
However, Netscape decided to implement some other language in its browser, a simple one, a scripting language. LiveScript was the first scripting language for the web. The syntax of LiveScript is based on Java, and it’s an interpreted language, not a compiled one like Java. Sun agreed with this language, and both companies decided to call it JavaScript.
Webpages visually enhanced, functionality increased, CGI scripts used more rarely, and all that with a programming language that is a lot easier to learn and makes programming simple things a lot easier.

JavaScript not Java

Many people tend to confuse JavaScript with Java.
Java is a high-level programming language used by advanced programmers, developed by Sun. Java code must be compiled before it can be used.
JavaScript is a scripting language for webpages. A scripting language is an easier programming language, usually originating from a high-level language. The JavaScript source code is interpreted by the browser plug-in. Compatibility is also greater.
Therefore, for small projects, or some small enhancements for a web page, JavaScript is better.

JScript

JS (abridge for JavaScript) is not JScript. After the success JavaScript had, Microsoft decided to implement JavaScript in its own browser, Internet Explorer. However, Netscape, who had the license for JavaScript decided to rent it, and not to sell it. Unsatisfied, Microsoft made his own JavaScript version from the public documentation only, called JScript. JScript is something different from JavaScript, but the syntax is almost the same.

Things to know about JS

ECMAScript

These standards make you have a big headache, at least at the beginning. Microsoft and Netscape handed the language to ECMA Organization for standardization. This is useful because it was very hard for a programmer to program make the script work in Internet Explorer and in Netscape Navigator also, or other browser. ECMAScript is the standard version of JavaScript and JScript. Almost all browsers recognize it.
After significant modifications were added by Microsoft and Netscape to their Java scripting language version, new versions are developed, and handed to ECMA Organization that makes one standard scripting language out of them, called ECMAScript.

VBScript

VBScript was some kind of reaction for JavaScript, coming from Microsoft. Based on the powerful and well-known programming language called Visual Basic, VBScript is most often used on big networks (intranets), and rarely used on the Internet, one disadvantaged for it is that Netscape and some other browsers don’t support it.

JS is Client-Side

There are two types of programming languages on the Internet, Server-side and Client-side.
Server-side languages are exclusive for servers, and use server resources; the result is being displayed on the client computer (the computer that’s accessing the webpage). A server-side language often used is PHP.
Client-Side languages are run on the client computer. Server doesn’t do much work in this case. JavaScript, VBScript, ActiveScript and many other languages are designed for running on the client computer.

Interpreted language

Basically, there are two types of programming languages:
Compiled languages require that after you write the code, to transform it in machine language (pure code that it’s very easy and fast to understand by the computer). Machine code is usually binary code. Every time you run the code (for testing), you must compile it with a compiler. After your application is finished, you can do a full compile and it will result an executable file (exe for example). There are advantages and disadvantages. Compiled languages run faster than interpreted language, are more stable, offer more control, the code is easier to protect… the only disadvantage is that when you develop your application and test it, you must compile it to run it, but it’s not a big problem, usually this doesn’t take more than a matter of seconds. All executable applications that you use, like MS Word, Outlook Express, Macromedia Dreamweaver… are compiled.
Interpreted language remains the same as you write it, and it’s interpreted by some software that transforms it in machine code in real time. This takes much longer but interpreted code is usually used in simpler languages, scripting languages, and regularly in web programming. There must be software to interpret the code you write, and in web programming, the software is the browser. It depends on the browser, if it can understand JavaScript.

Browsers

Most browsers are compatible with JavaScript. In addition, because the dominant browsers are Internet Explorer, Netscape and Opera, that support the latest version of JavaScript, you shouldn’t have problems with compatibility, with few exceptions.

Object-Oriented based language

JavaScript is a language based on objects. This is an advantage for powerful programmers. More about this and how to program O-O with JavaScript you’ll find out in further chapters.

Source code not protected

Because it’s not a compiled language, and it runs on client-side, your JS code is available to see by all your visitors, and steal it. The only way you can make it harder to be copied is to write your code in a separate file. Doing so doesn’t fully protect your code, and if the visitor really wants to see your code, he will. But more about this in the next chapters.

JS extends HTML

Plain text and images, with no dynamism characterizes HTML. Without scripting languages like JavaScript, HTML will be dull. And so it was continued till JavaScript.
JavaScript is used on webpages for different reasons. Even if it is for design or functionality, JavaScript code is used in many pages as an extension for HTML.

Event-driven language

Event-driven languages are those that need an event to trigger an action. For example, in a dialog box, to continue, or to stop, you must click OK or Cancel, respectively. If you click OK, the program will start and continue its work. If you click Cancel, it will exit. To start working or to exit are actions driven by the button you choose. If you don’t click anything, the program will do nothing. This is how an event-driven language acts.

What is it good for?

Until now we didn’t discussed more precise what’s JavaScript good for in webpages. I’ll try to show you some of the most things JavaScript is used for.

Validation

Text validation was inconvenient with good old CGI, and it’s still the same with PHP and other server-side languages. Let’s think at a form… just like the ones you see every day on the internet where they ask you to register or to login somewhere. If you misspell something, you don’t enter you ZIP Code in the correct format for example, the form will be sent to the server, you wait while the page loads (if the server is busy, you will wait…). Then the server processes the form, it sees that you didn’t complete it correctly, and sends you a page where you find out what misspell you have done, and that you must go back to correct it.
With JavaScript validation is a lot easier for the user, but for the server too, because it doesn’t generate so much useless traffic. Because it runs server side, you will be alerted immediately about the mistake, with no more pages loading.

Functional webpages

You can put current time on different places on earth if you have a news site or an advanced calculator if you have a science site, or one that works based on a formula.
If you know that your webpage doesn’t work well with a specific browser, you can make a script that will detect the browser and warn the user that the webpage isn’t designed for that browser, or for that screen resolution, or color depth. You can also find this kind of information about your visitors, including their IP address and who referred them to your webpage.
You can store cookies on the client’s computer, and when they visit the site again the script will know it’s the same visitor. This helps you a lot. It’s especially used at login systems. If the visitor is registered on the site, the next time he will visit the site, it will be logged in.

Eye-catching webpages

You can visually enhance a webpage with JavaScript. Further, you can combine attractiveness with functionality. For example you can make a pop-up (commercial or not) that moves around the screen and then stops in one corner. That will attract the visitor. You can also make one that when you close it, another one will appear, and then again… that will not attract the visitor. It will make him mad (probably he will be forced to reboot) and certainly he will not visit your page anymore. That’s a fast way to decrease your number of visitors, it’s harder to increase it, so be sure you’re not annoying your visitors with JavaScript, because many webmaster use JS in a negative way and they don’t know it.

JavaScript compatibility

Compatibility is an important thing, which a webmaster and programmer should pay attention to, though nowadays browsers tend to keep up with the newest versions, and compatibility doesn’t represent such a big problem no more.

JS and the browsers

We said earlier that most of the browsers support JavaScript, and an approximate recent statistic that I made up write now says that 95% of internet users use browsers that support JavaScript. You won’t have problems with this kind of compatibility, because dominant browsers are Internet Explorer, Netscape, Opera, and some other browsers specific for operating systems like Linux and OS2 for Macintosh. All of this support JavaScript at its latest version. It’s time to talk about JavaScript versions.

JavaScript versions

As I was saying earlier, ECMA Script helped a lot at making a good compatibility between browsers. Let’s see a table of all versions of JavaScript, and which browsers support that version.

JavaScript 1.0 – Only Netscape Navigator 2 was supporting the first version of JS because it was the idea of the people down at Netscape.

JavaScript 1.1 – It’s supported by Netscape Navigator 3, of course, Internet Explorer, because Microsoft realized the potential JavaScript had. Even Opera 3 supported JS from now on.

JavaScript 1.2 – Netscape Navigator 4 and Internet Explorer 4, which was implemented in Windows 95.

JavaScript 1.3 – As usual, Netscape Navigator, now at version 4.05 till 4.70, and Internet Explorer 5. ECMA Script is at its first version, 1.0.

JavaScript 1.4 – Netscape now implements it in Netscape Enterprise Server 4.0 and Microsoft in its new Internet Explorer 5.5. Mozilla also supports it from its early beta version.

JavaScript 1.5 – The latest and most used version of JS is implemented in the newest browsers versions. Also, at the time JavaScript 1.5 was launched, Microsoft’s JScript reached version 5.5.

This information is useful because you will probably want that your script to work with older browsers, that are still used. Of course, you won’t do scripts that work with Netscape Navigator 2, because probably no one is using this browser anymore. But you will try to make your scripts compatible with Internet Explorer 5.5, because there may be some users who use this older version of Internet Explorer, though there are no extreme differences between version 1.4 and 1.5 of JS, and definitely you won’t use the new function implemented in version 1.5 in all your scripts.

Site’s target visitors matters

You can make your scripts compatible depending on your target visitors. For example if you make a site for webdesigners, it shouldn’t be such important to make your script compatible with Internet Explorer 5 or Netscape 4, because you site’s target are webdevelopers, who work with this kind of web technologies, and they probably use the latest versions of the browsers.
On the other hand, if you make a script for a banner-exchange system, you never know on what site the script ends-up, maybe on a culinary site, where the user probably doesn’t have big knowledge about computers, so he doesn’t keep his computer updated.

Different intepretation

A browser can interpret a piece of JS code different from another, but this is the happy case, when it doesn’t give an error. Some browsers actually don’t do anything when they can handle a piece of code; others throw an unpleasant error, or display something strange. It’s better to test your script with the most used browsers, to see if your script works fine with them.

Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top