The Javascript Language

Posted by: Rea Maor In: Programming - Tuesday, May 8th, 2007

What a history Javascript has! Created originally in a dual-partnership by Netscape and Sun Microsystems, Javascript was founded originally to use to handle the loading and manipulation of embedded Java. No sooner was Javascript out than Microsoft set out on a campaign to undermine it, first by making its own “VBScript” (now abandoned) and a small support for “JScript”, which was a deliberately crippled version to suppress support for Javascript. This was all about the famous “browser wars”, now called to a truce after Netscape begat Mozilla which begat Firefox.

Meanwhile, Javascript has endured a checkered history. It is famous for being the language that 15-year-old script kiddies use to make annoying trick pages, such as browser-jacking, cascading pop-ups, click traps, and other malicious applications. But it also became the language that enabled businesses to turn a static document into an interactive program; it could be argued that Javascript ended up shaping E-commerce as we know it today. Then suddenly at the turn of the century, Javascript gained new fame as the key component of AJAX “Asynchronous Javascript and XML”, and started finding new use in interactive web applications all over Yahoo and Google.

Javascript, once dismissed as a toy, has now become the most essential language to understand in web programming. And the barrier to entry is low; all you need is any major web browser and you’re good to go!

If you’ve coded in C, C++, or Java, Javascript will feel very natural to you. It also has just a small hint of Perl in it’s flavor. Here’s the code smack-down:

Note that you would always have to wrap script tags around the outside of your Javascript code. Like so:

<script type="text/javascript">
// code goes here.


// Just start with a double slash.

Data declarations:

var x=y+z;
var my_string="hello";

Oddly, you have to declare a variable with the “var” command the first time you use it, but do not have to cast it as a type.

Output statement:

alert("I\'m another annoying pop-up message!");
document.write("Welcome to my web page!");

There’s various ways you can output messages, since it’s a web browser environment.

Input statement:

Not as straight-forward as with other languages. You have to temporarily drop into HTML:

<form action="receiving_URL" >
<input type="text" name="form_data" value="enter text here" />
<input type="submit" value="Submit" />

This would create the space for the user to type. Then the user hits the ‘submit’ button. Then your Javascript code on the receiving URL would jump back in and handle the data.


document.write(Math.pow(2, 8) + "<br />");

“Math.pow” calls the Math module, and ‘pow’ is the “power of” function. We call it this way without having to include the Math module.

File access:

This is NOT implemented in Javascript! You can use Javascript to call another language, such as Java, to read in a file. An example is here. This is more a matter of web-access security than anything else, since it runs on the client side; you wouldn’t want a web page to easily be able to read all of your files, nor would you want a cross-site script attack to use your own Javascript to read your password file. And so on. Writing to a file (other than generating the text in the current document) using Javascript is out of the question. The exception to this of course is reading, writing, setting, and deleting cookies.

Conditional statements:

if ((this_year % 4) == 0)

Pretty much like C. Switch statements are also supported:

switch (level)
case 1:
document.write("You have no special powers. <br />");
case 2:
document.write("You may moderate. <br />");
case 3:
document.write("You may edit other\'s comments. <br />");
case 4:
document.write("Dude! You have Magic Missile! <br />");

Fixed iteration:

var count;
var limit = new Array(1, 2, 3, 4, 5);
for (count in limit)
document.write("Number " + count + "<br />");

Note, by the way, that we use HTML break tags to designate new lines, just like we would add “\n” newline escapes in C or Python.

Conditional iteration:

var i=0;
while (i<=10)
document.write("The number is " + i);
document.write("<br />");


With an HTML tag:

<script src="/path/to/my/script.js" />


Typically, you would declare functions in the head of the HTML document, to ensure that they are read before they are called. Then call them from the body. An example of how the whole set-up looks:

<script type="text/javascript">
function doubler(x)
return x*2;
<script type="text/javascript">
document.write(doubler(10) + "<br />");

That’s the basics of the language! Because Javascript is designed to run in the special web browser environment, there are limitations to what it can do and extended features specific to it, when contrasted with application programming languages.

Javascript is one of the most famously-documented languages online, with examples and tutorials scattered everywhere.

To round out our tour, we will visit another very powerful language which also lives mostly on the web… in our next installment.

Related Posts:

2 Responses to “The Javascript Language”

  1. Wiktor Says:

    Nice article… but…

    if ((this_year % 4) == 0) “);
    Run it and you’ll see “Number 0….” if you want the correct behaviour use:
    for (count in limit)
    document.write(“Number ” + limit[count] + “”);

  2. Wiktor Says:

    aaah weird entities escaping… once more:
    Not the right way to calculate leap year: if ((this_year % 4) == 0) ‘

Leave a Reply