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

No comments:

Post a Comment