Java Script Class3
Java Script Class3
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;
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;
}
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.
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>
‘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.