Java Script-1
Java Script-1
if (condition)
{
lines of code to be executed if the condition is true
}
else
{
lines of code to be executed if the condition is false
}
<html>
<head>
<title>If...Else Statments!!!</title>
<script type="text/javascript">
// Get the current hours
var hours = new Date().getHours();
if(hours<12)
document.write("Good Morning!!!<br />");
else
document.write("Good Afternoon!!!<br />");
</script>
</head>
<body>
</body>
</html>
• If…Else If…Else statement
• You can use If….Else If….Else statement if you want to check more
than two conditions.
if (condition1)
{
lines of code to be executed if condition1 is true
}
else if(condition2)
{
lines of code to be executed if condition2 is true
}
else
{
lines of code to be executed if condition1 is false and condition2 is
false
}
<html>
<head>
<script type="text/javascript">
var one = prompt("Enter the first number");
var two = prompt("Enter the second number");
one = parseInt(one);
two = parseInt(two);
if (one == two)
document.write(one + " is equal to " + two + ".");
else if (one<two)
document.write(one + " is less than " + two + ".");
else
document.write(one + " is greater than " + two + ".");
</script>
</head>
<body>
</body>
</html>
Switch...Case
Statement
• The switch..case statement is an alternative to
the if...else if...else statement, which does almost
the same thing.
• The switch...case statement tests a variable or
expression against a series of values until it finds a
match, and then executes the block of code
corresponding to that match.
switch(x){
case value1:
// Code to be executed if x === value1
break;
case value2:
// Code to be executed if x === value2
break;
...
default:
// Code to be executed if x is different from all
values
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript Switch Case Statement</title>
</head>
<body>
<script>
var d = new Date();
switch(d.getDay()) {
case 0:
document.write("Today is Sunday.");
break;
case 1:
document.write("Today is Monday.");
break;
case 2:
document.write("Today is Tuesday.");
break;
case 3:
document.write("Today is Wednesday.");
break;
case 4:
document.write("Today is Thursday.");
break;
case 5:
document.write("Today is Friday.");
break;
case 6:
document.write("Today is Saturday.");
break;
default:
document.write("No information available for that day.");
break;
}
</script>
</body>
</html>
Looping Statements
• Loops are used to execute the same block of code
again and again, as long as a certain condition is
met.
• The basic idea behind a loop is to automate the
repetitive tasks within a program to save the time
and effort.
• while — loops executes a block of code as long as
the condition specified evaluates to true.
• do…while — loops executes a block of code once;
then the condition is evaluated. If the condition is
true, the statement is repeated as long as the
specified condition is true.
• for — loops executes a block of code until the
counter reaches a specified number.
• The while Loop
• The while loop loops through a block of code as long
as the specified condition evaluates to true. As soon
as the condition fails, the loop is stopped.
• The generic syntax of the while loop is:
while(condition)
{
// Code to be executed
}
<html >
<head>
<title>JavaScript While Loop</title>
</head>
<body>
<script>
var i = 1;
while(i <= 5) {
document.write("<p>The number is " + i + "</p>");
i++;
}
</script>
</body>
</html>
• do…while loop
• The do…while loop is very similar to while loop. The
only difference is that in do…while loop, the block of
code gets executed once even before checking the
condition.
• Syntax:
do
{
block of code to be executed
} while (condition)
<html>
<head>
<script type="text/javascript">
document.write("<b>Using do...while loops </b><br />");
var i = 2;
document.write("Even numbers less than 20<br />");
do
{
document.write(i + "<br />");
i = i + 2;
}while(i<20)
</script>
</head>
<body>
</body>
</html>
• for loop
• The parameters of the for loop statement have following
meanings:
• initialization — it is used to initialize the counter variables, and
evaluated once unconditionally before the first execution of the
body of the loop.
• condition — it is evaluated at the beginning of each iteration. If it
evaluates to true, the loop statements execute. If it evaluates to
false, the execution of the loop ends.
• increment — it updates the loop counter with a new value each
time the loop runs.
• Syntax:
for(initialization; condition; increment)
{
// Code to be executed
}
<html>
<head>
<script type="text/javascript">
var students = new Array("John", "Ann", "Aaron", "Edwin",
"Elizabeth");
document.write("<b>Using for loops </b><br />");
for (i=0;i<students.length;i++)
{
document.write(students[i] + "<br />");
}
</script>
</head>
<body>
</body>
</html>
JavaScript DOM
• The Document Object Model, or DOM for short, is a
platform and language independent model to represent
the HTML or XML documents.
• It defines the logical structure of the documents and the
way in which they can be accessed and manipulated by an
application program.
• In the DOM, all parts of the document, such as elements,
attributes, text, etc. are organized in a hierarchical tree-
like structure.
• HTML DOM which provides a standard interface for
accessing and manipulating HTML documents through
JavaScript.
JavaScript DOM
methods
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
</head>
<body>
<h1>Mobile OS</h1>
<ul>
<li>Android</li>
<li>iOS</li>
</ul>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<script>
// Selecting element with id mark
var match = document.getElementById("mark");
students[0] = "John";
students[1] = "Ann";
students[2] = "Kevin";
JavaScript Array
Methods
• length property –> If you want to know the number of elements
in an array, you can use the length property.
• prototype property –> If you want to add new properties and
methods, you can use the prototype property.
• reverse method –> You can reverse the order of items in an array
using a reverse method.
• sort method –> You can sort the items in an array using sort
method.
• pop method –> You can remove the last item of an array using a
pop method.
• shift method –> You can remove the first item of an array using
shift method.
• push method –> You can add a value as the last item of the array.
<html>
<head>
<title>Arrays!!!</title>
<script type="text/javascript">
var students = new Array("John", "Ann", "Aaron", "Edwin", "Elizabeth");
Array.prototype.displayItems=function(){
for (i=0;i<this.length;i++){
document.write(this[i] + "<br />");
}
}
document.write("students array<br />");
students.displayItems();
document.write("<br />The number of items in students array is " + students.length + "<br />");
document.write("<br />The SORTED students array<br />");
students.sort();
students.displayItems();
document.write("<br />The REVERSED students array<br />");
students.reverse();
students.displayItems();
document.write("<br />THE students array after REMOVING the LAST item<br />");
students.pop();
students.displayItems();
document.write("<br />THE students array after PUSH<br />");
students.push("New Stuff");
students.displayItems();
</script>
</head>
<body>
</body>
</html>
JavaScript Functions
• What is Function?
• A function is a group of statements that perform
specific tasks and can be kept and maintained
separately form main program.
• Advantages of using functions
• Functions reduces the repetition of code within a
program
• Functions makes the code much easier to maintain
• Functions makes it easier to eliminate the errors
Defining and Calling a
Function
• The declaration of a function start with the function
keyword, followed by the name of the function you
want to create, followed by parentheses i.e. ( ) and
finally place your function's code between curly
brackets { }.
• Syntax:
function functionName() {
// Code to be executed
}
• Once a function is defined it can be called (invoked)
from anywhere in the document, by typing its name
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Define and Call a Function</title>
</head>
<body>
<script>
// Defining function
function sayHello() {
document.write("Hello, welcome to this website!");
}
// Calling function
sayHello(); // Prints: Hello, welcome to this website!
</script>
</body>
</html>
Adding Parameters to
Functions
• You can specify parameters when you define your function
to accept input values at run time. The parameters work like
placeholder variables within a function; they're replaced at
run time by the values (known as argument) provided to
the function at the time of invocation.
• Parameters are set on the first line of the function inside
the set of parentheses, like this: