JavaScript I
JavaScript I
<script>
…..
</script>
OR
<script type = “text /JavaScript”>
……
</script>
Hiding Javascript from Old
Browsers
<html>
<body>
<script type="text/javascript">
<!--
document.write("Hello World!");
//-->
</script>
</body>
</html>
Displaying Output
document.write
\b Backspace
\f Formfeed
\n New line
\r Carriage return
\t Tab
\' Single quote that will nor terminate a sring
\" Double quote that will not terminate a string
\\ Single backslash character
Escape Sequences
• Example
alert(“We all say \n\”JavaScript \”\n is great”);
• Output
LESSON 2
If else
For loop
While loop
Do while loop
Array
Switch
If else
• A conditional statement
• For decision making
• if (comparison)
{ statements; }
• if (comparison)
{ statements; }
else
{ statements; }
• if (comparison)
{ statements; }
else if (comparison)
{ statements; }
else
{ statements; }
if else example
<script language = “JavaScript”>
var marks;
marks = prompt(“Please type in your marks”);
marks = parseInt(marks);
if (marks>=50)
{
alert(“Pass”);
}
else if(marks < 50)
{
alert(“Fail”);
}
</script>
if else example
<script language = "JavaScript">
var marks;
marks = prompt("Please type in your Test 1 marks");
marks = parseInt(marks);
if (marks<0)
{alert("Your marks is out of range");}
else if (marks <50&&marks >0)
{ alert("You obtained E"); }
</body>
</html>
for loop example 2
<html>
<head><title>Looping</title></head>
<body>
<script language=“JavaScript”>
var i;
</body>
Nested for loop
<html>
<head>
<title>My First JavaScript</title>
</head>
<body>
<script language= "JavaScript">
for (a=0;a<3;a++)
{
document.write("<p>a is " + a + "</p>");
for (b=1;b<3;b++)
{
document.write("welcome " + b + "<br>" );
}
}
</script></body></html>
Explanation:
The outer for loop iterates 3 times (a=0 until a=2) while
the inner for loop iterates 2 times only (b=1 until b=2).
While loop
while (condition)
{
statements;
}
statement block;
do
{
statements;
}
while (condition);
do while example
<html>
<head>
<title>while loop</title>
</head>
<body>
<script language= "JavaScript">
var i=1;
do
{
document.write("i is " + i);
}
while (i>5)
document.write("<br>do while loop will execute
at least once");
</script></body></html>
do while VS while loop
do
{
while (i>5)
document.write("i is " + i);
{
} document.write("i is " + i);
while (i>5) }
document.write("<br>do while loop will document.write("<br>do while loop will
execute at least once"); execute at least once");
Javascript Array
• An array is a variable that can contain multiple values
• A variable is given array status using the JS “new” keyword along with the JS
“Array()” constructor
• Every item in an array has its own index number. Index number starts with 0.
Array Declaration
• EXAMPLE 1
}
</script>
THANK YOU
Next lesson JavaScript II