Wednesday, January 12, 2011

JavaScript 101 Module 0

Write a simple JavaScript program which will print the sum of 2+2 and display the result in an alert window in the browser.


You will have to create an HTML page and put your JavaScript code within that page, so it executes when the page loads.


First way
<html>
<head>
<script language="JavaScript">
<!--
alert ("4")
//-->
</script>
</body>
</html>

Second way
<html>
<head>
<script language="JavaScript">
<!--
var a = "2";
var b = "2";
alert( Number(a) + Number(b) );
//-->
</script>
</body>
</html>

Third way

<html>
<head>
<script language="JavaScript">
<!--
alert( Number(2) + Number(2) );
//-->
</script>
</body>
</html>

last way but the most simple

<html>
<head>
<script language="Javascript">
<!--
alert( 2 + 2 );
//-->
</script>
</body<
</html>


I used this page to encapsulate the code.
Blogger is not to happy about having raw code so using this tool will save you a bunch of & l t ; & l t ; etc
If you need to know more about reserved characters read up

I used this for HTML tags.

I used this for the Math object Information.

Right from the horses mouth Core JavaScript Guide

JavaScript 101 Start here

This blog is a public version of the JavaScript class I am a facilitating with Parag from his website here.

The content I create is mirrored at my own website
I have to do this because some of the code will not work within the confines of Blogger. All the code will be here but will be executed from my website. When the course is complete I will pack it all up into a single file.If I get the time and the energy I will package into a WAMP and LAMP stack so you can install and run off your local machine.
I am giving a small plug for my hosting provider JTL
I have been with these folks for years and they are just the best,
For $7.50 US a month you can have your own Linux Server online.

Module 0