0% found this document useful (0 votes)
5 views

Java Script Class3

Javascript base

Uploaded by

akashpk655
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Java Script Class3

Javascript base

Uploaded by

akashpk655
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Control Structures in JavaScript

Control structures used in JavaScript are the same as that of C++ language.

1. if statement
This can be used for performing an operation if the given condition is true.
Syntax :
if (test_expression)
{
statements;
}

2. if...else statement
if...else can be used for performing a block of statements if the given condition
is true and another block of statements if the condition is false.
Syntax :
if (test_expression)
{
statements;
}
else
{
statements;
}

3. switch statement
Switch is a multi casing statement which execute statements based on a
matching value of the expression.
Syntax :
switch (expression)
{
case value1:
statements;

Client Side Scripting Using JavaScript – Class 3 Page 1


break;
case value2:
statements;
break;
……………………
……………………
default:
Statements;
}

4. for loop
For loop executes a block of statements repeatedly as long as the given
condition is true.
Syntax :
for (initialisation; test_expression; updation)
{
Statements;
}

5. while loop
While loop execute a block of statements repeatedly as long as the given
condition is true.
Syntax :
Initialisation;
while (test_expression)
{
Statements;
updation;
}

Client Side Scripting Using JavaScript – Class 3 Page 2


Example Program
Write the html document to create a web page that displays even numbers upto 100.
<HTML>
<HEAD>
<TITLE>Javascript – while loop</TITLE>
</HEAD>
<BODY>
<SCRIPT Language= "JavaScript">
var i;
i = 2;
while (i<=100)
{
document.write(i);
document.write("<BR>");
i += 2;
}
</SCRIPT>
</BODY>
</HTML>

Built-in functions
JavaScript provides a large number of built-in functions. These functions are
also called methods.

1. alert()
This function is used to display an alert message on the screen.
Eg: alert(“Your password is incorrect”);

2. isNaN()
This function is used to check whether an entered value is a number or not.
‘NaN’ stands for ‘Not a Number’. This function returns ‘true’ if the given value is not
a number.

Client Side Scripting Using JavaScript – Class 3 Page 3


3. toLowerCase()
This function is used to convert the given string to its lower case format ie;
capital letters will be converted to small letters.
Eg:
<script Language = “JavaScript”> Output
var x, y;
welcome to javascript
x= “Welcome to JavaScript”;
y= x.toLowerCase();
document.write(y);
</script>

4. toUpperCase()
This function is used to convert the given string to its upper case format ie;
small letters will be converted to capital letters.
Eg:
<script Language = “JavaScript”> Output
var x, y;
WELCOME TO JAVASCRIPT
x= “Welcome to JavaScript”;
y= x.toLowerCase();
document.write(y);
</script>

5. charAt()
This function returns the character at a prescribed position. charAt(0) returns
the first character in the string. charAt(5) returns the sixth character in the string.
Eg:
<script Language = “JavaScript”> Output
var S, n;
S= “JavaScript”; r
n= S.charAt(6);
document.write(n);
</script>

Client Side Scripting Using JavaScript – Class 3 Page 4


6. Number()
This function converts a string into its number type.

‘length’ Property
This property returns the length (number of characters) of a given string.

Eg:
<script Language = “JavaScript”> Output
var p, q;
p= “JavaScript”; 10
q= p.length;
document.write(q);
</script>

Note: The difference between a function and a property is that the function has parentheses
( and ) with parameters after the function name, but the property does not.

Client Side Scripting Using JavaScript – Class 3 Page 5

You might also like