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

Unit-II Web Technologies

Uploaded by

224aki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Unit-II Web Technologies

Uploaded by

224aki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

www.android.universityupdates.in | www.universityupdates.in | www.ios.universityupdates.

in

Unit-II
JavaScript
2.1 Introduction :
JavaScript is a very powerful client-side scripting language. JavaScript is used mainly for
enhancing the interaction of a user with the webpage. JavaScript is also being used widely in game
development and Mobile application development.
JavaScript was developed by Brendan Eich in 1995. The language was initially called LiveScript and
was later renamed JavaScript.
JavaScript is a Scripting language . It is a light-weighted and interpreted language and it is a case-
sensitive language.
Application of JavaScript
JavaScript is used to create interactive websites. It is mainly used for:
o Client-side validation,
o Dynamic drop-down menus,
o Displaying date and time,
o Displaying pop-up windows and dialog boxes (like an alert dialog box, confirm dialog box
and prompt dialog box) and Displaying clocks etc.
How to Add a Script to Your Pages :
JavaScript can either be embedded in a page or placed in an external script file (rather like CSS).
You add scripts to your page inside the <script> element. The type attribute on the opening
<script> tag indicates what scripting language will be found inside the element, so for JavaScript
you use the value text/JavaScript .

Syntax:-
<script type=”text/javascript”>
document.write(“content”);
</script>
In the above syntax ,
The script tag specifies that we are using JavaScript.
The text/javascript is the content type that provides information to the browser about the data.
The document.write() function is used to display dynamic content through JavaScript.
The write() method to add a new line of text into the web page (and the web page is represented
using the document object).
The < noscript > Element
The <noscript> tag in HTML is used to display the text for those browsers which does not support
script tag or the browsers disable the script by the user. This tag is used in both <head> and
<body> tag.
This tag is used in those browsers only which does not support scripts.
Syn : <noscript> content</noscript>
1

www.android.previousquestionpapers.com | www.previousquestionpapers.com | www.ios.previousquestionpapers.com


www.android.universityupdates.in | www.universityupdates.in | www.ios.universityupdates.in

Simple Program : Displaying a Line of Text in a web Page (Script inside the body tag)
<html>
<head>
<title> JavaScript Example </title>
</head>
<body>
<script type="text/javascript">
document.write("Welcome to JavaScript Programming")
</script>
</html>

You can use JavaScript code in two ways.


1. You can either include the JavaScript code internally within your HTML document itself
We can write the script tag with in the HTML
• inside the body tag ,
• inside the head section,
• out of body and head section but within HTML.
We can write script tag out of HTML.

2. You can keep the JavaScript code in a separate external file and then point to that file from
your HTML document.

• An external JavaScript file must be saved by .js extension. It is recommended to embed all
JavaScript files into a single file. It increases the speed of the webpage.

External.js
document.write("Welcome to JavaScript Programming")

External.html
<html>
<head>
<title> JavaScript Example </title>
</head>
<body>
<script src=”external.js”>
</script>
</html>

www.android.previousquestionpapers.com | www.previousquestionpapers.com | www.ios.previousquestionpapers.com


www.android.universityupdates.in | www.universityupdates.in | www.ios.universityupdates.in

2.2 Variable / Memory Concepts :


A variable is used to store some information. JavaScript includes variables which hold the data
value and it can be changed anytime.

JavaScript uses reserved keyword var to declare a variable. A variable must have a unique name.
You can assign a value to a variable using equal to (=) operator when you declare it or before
using it.

Syntax: var variablename; // Variable Declaration

var variablename=value; //Variable Declaration & Initialization


Example:
var a=1; // variable “a” stores numeric value
var c=”avanthi” ; //variable “c” stores string value
var b; // declared variable without assigning a value
If no value is given to variable, then its value is undefined .
Lifetime of a Variable
If you declare a variable using the var keyword inside a function, it can be accessed only in that
function. After the function has run, we cannot access that variable. Variables in functions are
called local variables .

If you declare a variable outside a function, all the functions on your page can access it . The
lifetime of these variables starts when they are declared and ends when the page is closed.
Variables outside a functions are called global variables.

Example program on variable:


<html>
<head>
<title>variable</title>
<script>
var a=20;
document.write("a value is "+a+"<br>");
var b=40.2;
document.write("b value is "+b+"<br>");
var st="Hello Avanthi";
document.write("<br> string value is "+st);
document.write("<br> Boolean value is "+(60>5));
var z;
document.write("<br> z value is "+z);
</script>
</head> </html>
3

www.android.previousquestionpapers.com | www.previousquestionpapers.com | www.ios.previousquestionpapers.com


www.android.universityupdates.in | www.universityupdates.in | www.ios.universityupdates.in

2.3 Prompt dialog, Confirm and alert boxes

JavaScript provides three important Dialog Boxes, which include Alert Dialog Box for users,
Confirmation Dialog Box, and Prompt Dialog Box.
• Alert Dialog box
• Confirmation Dialog box
• Prompt Dialog box

2.3.1 Alert Dialog box


Alert Dialog Box is mainly used to display a notice, warning, or error
to users. Basically, you cannot customize dialog box icon or title, ... you can only provide the message
that the dialog box will display.
In addition, Alert Dialog Box has only one OK button to close a dialog box.

Example Program on alert method:

<html>
<head>
<title>Alert popup</title>
<script>
window.alert("Welcome to JavaScript Programming");
</script>
</head>
</html>

2.3.2 Prompt Dialog Box


Prompt Dialog Box is used for users to enter an information. This
dialog box is very simple. It includes a Text Field for users to enter information.
The dialog box has 2 OK and Cancel buttons.
If an user clicks OK, the prompt method returns contents on Text Field, otherwise,
if the user clicks Cancel, the prompt method returns null.

www.android.previousquestionpapers.com | www.previousquestionpapers.com | www.ios.previousquestionpapers.com


www.android.universityupdates.in | www.universityupdates.in | www.ios.universityupdates.in

Example Program on prompt method:

<html>
<head>
<title>prompt popup</title>
<script>
var res=window.prompt("enter user name");
window.alert("User name is "+res);
</script>
</head>
</html>

www.android.previousquestionpapers.com | www.previousquestionpapers.com | www.ios.previousquestionpapers.com


www.android.universityupdates.in | www.universityupdates.in | www.ios.universityupdates.in

2.3.3 Confirmation Dialog Box


Confirmation Dialog Box is used to ask the user to confirm
something. This dialog is very simple, you cannot customize the icon or the title of the dialog box, you
can only provide a message asking the user to confirm.
This dialog box has 2 OK and Cancel buttons.

To display a Confirmation Dialog Box you call the confirm(message) function, in which
the message is one requesting an user to confirm.
If the user clicks the OK button, this function returns true, otherwise if the user clicks the No button,
this function returns false.

www.android.previousquestionpapers.com | www.previousquestionpapers.com | www.ios.previousquestionpapers.com


www.android.universityupdates.in | www.universityupdates.in | www.ios.universityupdates.in

2.4 Operators
An operator performs some operation on single or multiple operands (data value) and produces a
result.
For example 1 + 2, where + sign is an operator and 1 is left operand and 2 is right operand.
+ operator adds two numeric values and produces a result which is 3 in this case.
JavaScript includes following categories of operators.
1. Arithmetic Operators
2. Assignment Operators
3. Comparison Operators
4. Logical Operators
5. String Operators
6. Conditional Operator
2.4.1 Arithmetic Operators
Arithmetic operators perform arithmetic operations upon operands
Symbol Description Example (x=10 and y=3) Result
+ Addition x+y 15
- Subtraction x-y 8
* Multiplication x*y 30
/ Division x/y 5
% Modulus (division remainder) x%y 1

Example Program on Arithmetic Operator


<html>
<head>
<title>Arithmetic Operator</title>
</head>
<body>
<script>
var a=12,b=3
document.write("Addition of a & b : "+(a+b))
document.write("<br>Subtraction of a & b : "+(a-b))
document.write("<br>Multiplication of a & b : "+(a*b))
document.write("<br>Division of a & b : "+(a/b))
document.write("<br>Modulo of a & b : "+(a%b))
</script>
</body>
</html>

www.android.previousquestionpapers.com | www.previousquestionpapers.com | www.ios.previousquestionpapers.com


www.android.universityupdates.in | www.universityupdates.in | www.ios.universityupdates.in

2.4.2 Assignment Operators


The assignment operators are the operators used to assign the right-hand
side value to the left-hand side variable.
The following table presents the list of assignment operations in JavaScript along with their description.

Symbol Example Using Shorthand Equivalent Without Shorthand


+= x+=y x=x+y
-= x - =y x=x - y
*= x*=y x=x*y
/= x/=y x=x/y
%= x%=y x=x%y

Example Program on Assignment Operator


<html>
<head>
<title>Assignment Operator</title>
</head>
<body>
<script>
var a=12,b=3
document.write("a+=b : "+(a+=b))
document.write("<br>a-=b : "+(a-=b))
document.write("<br>a*=b : "+(a*=b))
document.write("<br>a/=b : "+(a/=b))
document.write("<br>a%=b : "+(a%=b))
</script>
</body>
</html>

www.android.previousquestionpapers.com | www.previousquestionpapers.com | www.ios.previousquestionpapers.com


www.android.universityupdates.in | www.universityupdates.in | www.ios.universityupdates.in

2.4.3 Comparison Operators


comparison operators compare two operands and then return either true or false based on
whether the comparison is true or not.

Symbol Description Example


== Equal to 1==2 returns false
3==3 returns true
!= Not equal to 1!=2 returns true
3!=3 returns false
> Greater than 1 > 2 returns false
3 > 3 returns false
3 > 2 returns true
< Less than 1 < 2 returns true
3 < 3 returns false
3 < 1 returns false
>= Greater than or equal to 1 > =2 returns false
3 > =2 returns true
3 > =3 returns true
<= Less than or equal to 1 < =2 returns true
3 < =3 returns true
3<=2 returns false

Example Program on Comparison Operator

<html>
<head>
<title>Comparison Operator</title>
</head>
<body>
<script>
var x=12,y=3
document.write("x==y : "+(x==y))
document.write("<br>x!=y : "+(x!=y))
document.write("<br>x>y : "+ (x>y))
document.write("<br>x>=y : "+(x>=y))
document.write("<br>x<=y : "+(x<=y))
document.write("<br>x less than y : "+ (x<y))
</script>
</body>
</html>

www.android.previousquestionpapers.com | www.previousquestionpapers.com | www.ios.previousquestionpapers.com


www.android.universityupdates.in | www.universityupdates.in | www.ios.universityupdates.in

2.4.4 Logical or Boolean Operators


Logical or Boolean operators return one of two values: true or false . They are particularly helpful
when you want to evaluate more than one expression at a time.

Operator Name Description Example (where x =1 and y =2)


&& And Allows you to check if both of (x < 2 & & y > 1) Returns true
two conditions are met (because both conditions are met)
?? Or Allows you to check if one of (x < 2 ?? y < 2) Returns true
two conditions are met (because the first condition is met)
! Not Allows you to check if ! (x > y) Returns true
something is not the case (because x is not more than y)

Example Program on Logical Operator


<html>
<head>
<title>Logical Operator</title>
</head>
<body>
<script>
var x=12,y=3, z=10
document.write("Logical and : "+(x>y && x>z))
document.write("<br> Logical OR : "+(x>y || x>z))
document.write("<br> Logical Not : "+ !(x>y))
</script>
</body>
</head>
</html>

10

www.android.previousquestionpapers.com | www.previousquestionpapers.com | www.ios.previousquestionpapers.com


www.android.universityupdates.in | www.universityupdates.in | www.ios.universityupdates.in

2.4.5 String Operator (Using + with Strings)


The process of adding two strings together is known as concatenation . You can also add text to
strings using the + operator.
Example Program on String Operator
<html>
<head>
<title>String Operators</title>
</head>
<body>
<script type="text/javaScript">
var a="Hello";
var b="world",c;
c=a+b;
document.write("String Concatenation : "+c);
</script>
</body>
</html>

2.4.5 Conditional Operator / Ternary Operator


A ternary operator evaluates a condition and executes a block of code based on the
condition.
syntax is:
condition ? expression1 : expression2

Example Program on Conditional / Ternary Operator


<html>
<head>
<title>Conditional Operators</title>
</head>
<body>
<script type="text/javaScript">
var age=20
var result = (age>18) ?'Elgible':'Not elgibe';
document.write(result+" to Vote");
</script>
</body>
</html>
11

www.android.previousquestionpapers.com | www.previousquestionpapers.com | www.ios.previousquestionpapers.com


www.android.universityupdates.in | www.universityupdates.in | www.ios.universityupdates.in

2.5 Conditional Statements


Conditional statements allow you to take different actions depending upon different
statements.There are three types of conditional statements.
2.5.1 if Statements
if statements allow code to be executed when the condition is true.
Syntax:

if (condition)
{
code to be executed if condition is true
}

Example Program on if statement :


<html>
<head>
<title>if statement</title>
</head>
<body>
<script type="text/javaScript">
var age=18;
if(age>=18)
{
document.write("Eligible to vote");
}
</script>
</body>
</html>

2.5.2 if ..else Statements

if statements allow code to be executed when the condition is true, else statements allow to
executed when condition is false.
Syntax:

if (condition)
{
code to be executed if condition is true
}
else
{
code to be executed if condition is false
}
12

www.android.previousquestionpapers.com | www.previousquestionpapers.com | www.ios.previousquestionpapers.com


www.android.universityupdates.in | www.universityupdates.in | www.ios.universityupdates.in

Example Program on if ..else statement :


<html>
<head>
<title>if..else statement</title>
</head>
<body>
<script >
var age=15;
if(age>=18)
{
document.write("Eligible to vote");
}
else
{
document.write("Not Eligible to vote");
}
</script>
</body>
</html>

2.5.3 nested if Statements

Writing an if statement inside another if-statement is called nested if statement.


The general syntax of the nested if-statement is as follows

if(condition_1)
{
if(condition_2)
{
inner if-block of statements;
...
}
...
}

13

www.android.previousquestionpapers.com | www.previousquestionpapers.com | www.ios.previousquestionpapers.com


www.android.universityupdates.in | www.universityupdates.in | www.ios.universityupdates.in

Example Program on nested if statement :

<html>
<head>
<title>Nested if </title>
</head>
<body>
<script>
var x=10,y=20
if(x>5)
if(y>5)
document.write("x and y are > 5 ")
else
document.write("x is <= 5")
</script>
</body>
</html>

2.5.4 nested if..else Statements /if else if


Writing an if-statement inside else of an if statement is called if-else-if statement.
The general syntax of the an if-else-if statement is as follows.

if(condition_1) {
condition_1 true-block;
...
}
else if(condition_2){
condition_2 true-block;
condition_1 false-block too;
...
}

Example Program on nested if else / if else if statement :

<html>
<head>
<title>if else if </title>
</head>
<body>
14

www.android.previousquestionpapers.com | www.previousquestionpapers.com | www.ios.previousquestionpapers.com


www.android.universityupdates.in | www.universityupdates.in | www.ios.universityupdates.in

<script>
var a=15
if(a==10)
document.write("a is equal to 10")
else if (a==15)
document.write("a is equal to 15")
else if (a==25)
document.write("a is equal to 25")
else
document.write("a is not equal to 10, 15 or 25")
</script>
</body>
</html>

2.5.5 switch statement

The switch is a conditional statement like if statement. Switch is useful when you want to execute
one of the multiple code blocks based on the return value of a specified expression.
Syntax:

switch (expression)
{
case option1:
code to be executed if expression is what is written in option1
break;
case option2:
code to be executed if expression is what is written in option2
break;
default:
code to be executed if expression is different from option1,and option2,
}

Example Program on switch statement:


<html>
<head>
<title>Switch Case</title>
</head>
<body>
<script type="text/JavaScript">
15

www.android.previousquestionpapers.com | www.previousquestionpapers.com | www.ios.previousquestionpapers.com


www.android.universityupdates.in | www.universityupdates.in | www.ios.universityupdates.in

var a=2;
switch(a)
{
case 1: document.write("Case 1 Executed");
break;
case 2: document.write("Case 2 Executed");
break;
case 3: document.write("Case 3 Executed");
break;
default:document.write("default case Executed");
}
</script>
</body>
</html>

2.6 Looping
Looping statements are used to execute the same block of code a specified number of times.
2.6.1 While
In a while loop, a code block is executed if a condition is true and for as long as that condition
remains true.
Syntax:

while (condition)
{
code to be executed
}
Example 1 Program on while loop:
<html>
<head>
<title>while Loop</title>
</head>
<body>
<script type="text/JavaScript">
var i=1;
while(i<11)
{
document.write(i+"*3="+(i*3)+"<br>");
i++;
}
</script>
</body>
</html>
16

www.android.previousquestionpapers.com | www.previousquestionpapers.com | www.ios.previousquestionpapers.com


www.android.universityupdates.in | www.universityupdates.in | www.ios.universityupdates.in

Example 2 Program on while loop:

<html>
<head>
<title>while Loop</title>
</head>
<body>
<script>
let i=1;
while(i<10)
{

document.write("<br>i="+i);
i=i+1;
}
</script>
</body>
</html>

2.6.2 do . . . while
The do-while loop is similar to while loop the only difference is it evaluates condition expression
after the execution of code block. So do-while loop will execute the code block at least once.

Syntax:

do
{
code to be executed
} while (condition)
Example 1 Program on do-while loop:
<html>
<head>
<title>do-while Loop</title>
</head>
<body>
<script type="text/JavaScript">
var i=1;
do
{
document.write(i+"*5="+(i*5)+"<br>");
i++;
} while(i<11)
17

www.android.previousquestionpapers.com | www.previousquestionpapers.com | www.ios.previousquestionpapers.com


www.android.universityupdates.in | www.universityupdates.in | www.ios.universityupdates.in

</script>
</body>
</html>

Example 2 Program on do-while loop:

<html>
<head>
<title>do-while Loop</title>
</head>
<body>
<script>
let i=1;
do
{
document.write("<br>i="+i);
i=i+1;
}while(i<10)
</script>
</body>
</html>

2.6.7 For
for loop to execute code repeatedly.
Syntax:

for(initializer; condition; iteration)


{
// Code to be executed
}

Example 1 Program on for loop:

<html>
<head>
<title>for Loop</title>
</head>
<body>
<script type="text/JavaScript">
var i;
for(i=1;i<11;i++)
{
document.write(i+"*2="+(i*2)+"<br>");
18

www.android.previousquestionpapers.com | www.previousquestionpapers.com | www.ios.previousquestionpapers.com


www.android.universityupdates.in | www.universityupdates.in | www.ios.universityupdates.in

}
</script>
</body>
</html>

Example 2 Program on for loop:

<html>
<head>
<title>For Loop</title>
</head>
<body>
<script>
for(let i=1;i<10;i++)
{
document.write("<br>i="+i);
}
</script>
</body>
</html>
2.7 Break and continue
• The break statement in javaScript is used to terminate a conditional or looping statement.
• The continue statement is used to move the execution control to the beginning of the looping
statement. When the continue statement is encountered in a looping statement, the execution
control skips the rest of the statements in the looping block and directly jumps to the beginning
of the loop.
Example program on break and continue
<html>
<head>
<title>break and continue</title>
</head>
<body>
<script type="text/JavaScript">
var i;
for(i=1;i<11;i++)
{
if (i==6)
break;
else if (i==3)
continue;
19

www.android.previousquestionpapers.com | www.previousquestionpapers.com | www.ios.previousquestionpapers.com


www.android.universityupdates.in | www.universityupdates.in | www.ios.universityupdates.in

else
document.write(i+"<br>");
}
</script>
</body>
</html>

2.7.1 Labeled Break and continue


The Label Statement is used with the break and continue statements and serves to
identify the statement to which the break and continue statements apply.
Without the use of a labeled statement the break statement can only break out of a loop or
a switch statement. Using a labeled statement allows break to jump out of any code block.

Syntax
labelname:
statements
Example Program on Labeled break:

<html>
<head>
<title>Using break statement with Label</title>
</head>
<body>
<script>
stop:
for(var i=0;i<10;i++)
{
for(var j=0;j<5;j++)
{
if(i==3)
break stop;
document.write("*");
}
document.write("<br>");
}
</script>
</body>
</html>

20

www.android.previousquestionpapers.com | www.previousquestionpapers.com | www.ios.previousquestionpapers.com


www.android.universityupdates.in | www.universityupdates.in | www.ios.universityupdates.in

Example Program on Labeled continue :

<html>
<head>
<title>Using continue statement with Label</title>
</head>
<body>
<script>
nextrow:
for(var i=0;i<=5;i++)
{
document.write("<br>");
for(var j=1;j<=5;j++)
{
if(j>i)
continue nextrow;
document.write("*");
}
}
</script>
</body>
</html>

2.8 Functions / Program modules


JavaScript provides functions similar to most of the scripting and programming languages.
In JavaScript, a function allows you to define a block of code, give it a name and then execute it
as many times as you want.
A JavaScript function can be defined using function keyword.
//defining a function
function function-name(args )
{
// code to be executed
};
//calling a function
function-name(args);

21

www.android.previousquestionpapers.com | www.previousquestionpapers.com | www.ios.previousquestionpapers.com


www.android.universityupdates.in | www.universityupdates.in | www.ios.universityupdates.in

Example program on functions:


<html>
<head>
<title>Variable</title>
<script type="text/javaScript">
function display()
{
document.write("Functions in javascript");
}
display();
</script>
</head>
</html>

The Return Statement


Return statement is used to return a value or a result. This statement is used to specify the
value that is returned when a function is called.

Example 1 program on return statement:


<html>
<head>
<title>Return Statement</title>
<script type="text/javaScript">
function calculateArea(width,height)
{
area=width*height;
return area;
}
document.write("area of Rectangle "+calculateArea(10,20));
</script>
</head>
</html>

Example program find the square the numbers from 1 to 10

<html>
<head>
<title>Square Function</title>
</head>
<body>
<script>
document.write("<h2> Square the numbers from 1 to 10 </h2>");

22

www.android.previousquestionpapers.com | www.previousquestionpapers.com | www.ios.previousquestionpapers.com


www.android.universityupdates.in | www.universityupdates.in | www.ios.universityupdates.in

function square(y)
{
return y*y
}
for(var x=1;x<=10;x++)
document.write("<br> The square of " + x +" is "+square(x));
</script>
</body>
</html>

WAP to find Maximum of three numbers

<html>
<head>
<title>Maximum of Three Numbers</title>
</head>
<body>
<script>
var a=window.prompt("Enter First number");
var b=window.prompt("Enter Second number");
var c=window.prompt("Enter Third number");
document.writeln("First number: "+a);
document.writeln("<br>Second number: "+b);
document.writeln("<br>Third number: "+c);
function maximum(x,y,z)
{
return Math.max(x,Math.max(y,z));
}
23

www.android.previousquestionpapers.com | www.previousquestionpapers.com | www.ios.previousquestionpapers.com


www.android.universityupdates.in | www.universityupdates.in | www.ios.universityupdates.in

var maxvalue=maximum(a,b,c);
document.writeln("<br> Maximum is: "+maxvalue);
</script>
</body>
</html>

2.9 Scope Rules :-

Scope in JavaScript defines accessibility of variables, objects and functions.


There are two types of scope in JavaScript.

1. Global scope
2. Local scope

Global Scope
Variables declared outside of any function become global variables. Global variables
can be accessed and modified from any function.
Local Scope
Variables declared inside any function with var or let keyword are called local variables.
Local variables cannot be accessed or modified outside the function declaration.

24

www.android.previousquestionpapers.com | www.previousquestionpapers.com | www.ios.previousquestionpapers.com


www.android.universityupdates.in | www.universityupdates.in | www.ios.universityupdates.in

Example Program on Local & Global Variables

<html>
<head>
<title>Scope & Global Variables </title>
<script>
var x=1;
function start()
{
var y=5;
document.write("Local Variable y value :"+y);
A();
}
function A()
{
document.write("<br>Global Variable x value :"+x);
}
</script>
</head>
<body onload="start()">
</body>
</html>

25

www.android.previousquestionpapers.com | www.previousquestionpapers.com | www.ios.previousquestionpapers.com


www.android.universityupdates.in | www.universityupdates.in | www.ios.universityupdates.in

3.0 JavaScript Global Functions :

Javascript provides seven global functions. Object is one such global constructor
function in JavaScript which is used to new create objects.

escape() :-
The escape() function takes a string as a parameter and encodes it so that it can be
transmitted to any computer in any network which supports ASCII characters.
Syntax:
escape(string)
eval() :-
The eval() function in JavaScript is used to evaluate the expression, which evaluates the
specified string as JavaScript code and executes it.
Syntax:
eval(string)
isFinite() :-
The JavaScript number isFinite() method determines whether the given value is a finite
number. It returns true if the value is a finite number, otherwise it returns false.
Syntax:
Number.isFinite(num)
isNaN() :-
isNaN() method returns true if a value is Not-a-Number. Number.isNaN() returns true if
a number is Not-a-Number

Syntax:
isNaN(value)
parseFloat () :-
The JavaScript number parseFloat() method parses a string argument and converts it
into a floating point number. It returns NaN, if the first character of given value cannot be converted
to a number.

Syntax:
Number.parseFloat(string)
parseInt () :-
The JavaScript number parseInt() method parses a string argument and converts it into
an integer value. With string argument, we can also provide radix argument to specify the type of
numeral system to be used.

Syntax: Number.parseInt(string, radix)

26

www.android.previousquestionpapers.com | www.previousquestionpapers.com | www.ios.previousquestionpapers.com


www.android.universityupdates.in | www.universityupdates.in | www.ios.universityupdates.in

string - It represents the string to be parsed.

radix - It is optional. An integer between 2 and 36 that represents the numeral system to be used.

unescape() :-
The unescape() function takes a string as a parameter and returns a string in which all
characters previously encoded with escape are decoded
Syntax:
unescape(string)

<html>
<head>
<title>Global Functions</title>
<script>
document.write(escape("Javascript global functions"));
document.write(unescape("<br>Javascript global functions"));
var a=10, b=20, c=30;
document.write("<br>"+eval("a+b+c"));
document.write("<br>"+parseFloat("10"));
document.write("<br>"+parseInt("10.56"));
document.write("<br>"+isFinite(123));
document.write("<br>"+isNaN(“Hello”));
</script>
</head>
</html>

27

www.android.previousquestionpapers.com | www.previousquestionpapers.com | www.ios.previousquestionpapers.com


www.android.universityupdates.in | www.universityupdates.in | www.ios.universityupdates.in

3.1 Recursion :

Recursion is a process of calling itself. A function that calls itself is called a recursive function.

Here, the recurse() function is a recursive function. It is calling itself inside the function.
• A recursive function must have a condition to stop calling itself. Otherwise, the function is called
indefinitely.
• Once the condition is met, the function stops calling itself. This is called a base condition.
• To prevent infinite recursion, you can use if...else statement (or similar approach) where one
branch makes the recursive call, and the other doesn't.
So, it generally looks like this.

function recurse()
{
if(condition)
{
recurse();
}
else
{
// stop calling recurse()
}
}
recurse();

28

www.android.previousquestionpapers.com | www.previousquestionpapers.com | www.ios.previousquestionpapers.com


www.android.universityupdates.in | www.universityupdates.in | www.ios.universityupdates.in

<html>
<head>
<title>Recursive Factorial Function</title>
</head>
<body>
<script>
document.write("<h3> Factorial of 1 to 10 </h3>");
function factorial(n)
{
if(n<=1)
return 1;
else
return n*factorial(n-1);
}
for(var i=0;i<=10;i++)
document.write("<br>"+ i +"! &nbsp&nbsp&nbsp"+factorial(i));
</script>
</body>
</html>

29

www.android.previousquestionpapers.com | www.previousquestionpapers.com | www.ios.previousquestionpapers.com


www.android.universityupdates.in | www.universityupdates.in | www.ios.universityupdates.in

3.2 Recursion vs Iteration

Recursion Iteration
In iteration, there is a repeated
execution of the set of instructions.
Recursion is the process of calling a In Iteration, loops are used to
function itself within its own code. execute the set of instructions
repetitively until the condition is
false.
The format of iteration includes
There is a termination condition is initialization, condition, and
specified. increment/decrement of a
variable.
The termination condition is Here, the termination condition is
defined within the recursive defined in the definition of the
function. loop.

It is always applied to functions. It is applied to loops.

It is slower than iteration. It is faster than recursion.


It uses more memory as compared It uses less memory as compared to
to iteration. recursion

Unit-II Questions
1. Explain about JavaScript
2. Write a short note on Dialog box / Pop-up boxes (alert, prompt & confirm
3. Explain about Operators in JavaScript
4. Explain about Conditional statements with an example programs
5. Explain about Looping statements with an example programs
6. Explain about break, continue, Labeled break and continue statements
7. Explain in detail about functions
8. Explain about variables & scope rules in JavaScript
9. Global functions in JavaScript
10. Recursion
11. Recursion vs iteration

30

www.android.previousquestionpapers.com | www.previousquestionpapers.com | www.ios.previousquestionpapers.com

You might also like