JavaScript
From Wikipedia, the free encyclopedia
JavaScript is a scripting language for computers. It is often run in web browser applications to create dynamic content like a popup message or a live clock. It is not related to the programming language Java.
Example[change | change source]
This script writes "Example" on the screen. The lines that start with // are comments; they show someone reading the code what the code does.
<script type="text/javascript">
function example()
{
var ex = document.createTextNode('Example'); //make the computer remember "Example",
//so whenever you say "ex" the computer will append it with "Example"
document.body.appendChild(ex); //put the text on the bottom of the webpage
}
example();
/*
* The code below does almost the same thing as the code above,
* but it shows "Example" in a popup box and is shorter.
*
* This is a comment too, by the way.
*/
alert("Example");
</script>
The JavaScript is enclosed by <script></script> tags, to tell that it is a script and not text to be put onto the web page the JavaScript is running on. This script inserts the numbers 1 through 10 at the bottom of a webpage:
<script type="text/javascript">
for(var numOfTimesAround = 1; numOfTimesAround <= 10; numOfTimesAround++){
document.body.innerHTML = document.body.innerHTML + numOfTimesAround + "<br>";
/*
* This puts the number, then a new line, at the end of the web page.
* In javascript, using the + sign combines two words together.
* So writing "Hello" + " World" would make "Hello World".
* Or, writing 1 + "<br>" makes "1<br>", which is what we want.
*/
}
</script>
The for() loop makes whatever code is between the { and the } happen more than one time. In this case, it keeps looping until numOfTimesAround is equal to 10, then it stops.
Differences between Java and Javascript[change | change source]
- In Java, to make a variable (to make a computer remember something), you have to say what type of variable it is: a number, a word, a letter, or more. In JavaScript, this is not necessary.
- In JavaScript, functions are stored as variables (unlike Java). This makes the following code okay in JavaScript:
function sayHi(){
alert("hi!");
}
sayHi = function(){
alert("Bye!");
}
sayHi();
//alerts "Bye!" instead of "hi!" without giving an error
- JavaScript runs in a web browser, but Java runs directly on a computer.
- JavaScript is interpreted, but Java, in most cases, must be compiled.
Related pages[change | change source]
Other websites[change | change source]
- Learn JavaScript on the Mozilla Developer Center
- Mozilla's Official Documentation on JavaScript
- Video - Firefox 2 and Javascript with Mozilla Corp and JavaScript creator Brendan Eich
- References for Core JavaScript versions: 1.5
- New in JavaScript: 1.7, 1.6
- List of JavaScript releases: versions 1.0 - 1.7
- Brendan's Roadmap Updates: JavaScript 1, 2, and in between - the author's blog entry
- comp.lang.javascript FAQ Official FAQ for the comp.lang.javascript Usenet group
- RFC 4329, a document for the registration of media types related to ECMAScript and JavaScript. The current recommendations are "application/javascript" and "application/ecmascript", although neither is recognized by Internet Explorer.

