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

UNIT I Basics of JavaScript Programming

Uploaded by

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

UNIT I Basics of JavaScript Programming

Uploaded by

NTR Thor OP
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Program: Computer Engg

Class : TY
Course : Client Side Scripting
Language(Elective)
Unit I : Basics of JavaScript
Programming
Lecture 01: Basics of JavaScript
Programming

1
02
Client Side Scripting Language

Prof. Vishal B. Ohol


Experience : 10 Years
ME(Software Engg)

2
Departmentwww.sandipuniversity.edu.in
of Computer Engineering www.sandipfoundation.org
Form and Event Handling 03

Unit Outcomes(UOs)
1a. Create object to solve the given problem.
1b. Develop JavaScript to implement the switch-case statement for the given problem
1c. Develop JavaScript to implement loop for solving the given iterative problem.
1d. Display properties of the given object using letters and setters.
1e. Develop program using basic features of JavaScript to solve the given problem.

Contents
1.1 Features of JavaScript.
1.2 Object Name, Property, method, Dot syntax, main event.
1.3 Values and Variables.
1.4 Operators and Expressions – Primary expressions, Object and Array initializers, function
definition expression, property access expressions, invocation expressions.
1.5 If Statement, if…else, if….elseif, nested if statement.
1.6 Switch…case statement.
1.7 Loop statement – for loop, for…in loop, while loop, continue statement.
1.8 Querying and setting properties and deleting properties, property getters and setters.

3
Department of Computer Engineering www.sandipfoundation.org
Teaching and examination Scheme 04

Unit Outcomes(UOs)
• Create object to solve the given problem.
• Develop JavaScript to implement the switch-case statement for the given problem.
• Develop JavaScript to implement loop for solving the given interactive problem.
• Display properties of the given object using getters and setters.
• Develop program using basic features of JavaScript to solve the given problem.

4
Department of Computer Engineering www.sandipfoundation.org
Basis of Comparison between Programming vs Scripting 04

Parameters Programming Scripting


Examples Java, C, C++ etc JavaScript, Perl, Python etc
Execution Compiled Interpreted
Easier to learn than a programming
Learning Can take a significant amount of time
language
Less code intensive as compared to a
Nature Code intensive
traditional Programming language
Create binaries or executables which Does not create any binary files or
Requirement
execute from system’s memory executables, no memory will be allocated
Can be a tricky affair for a beginner to Easier to write and understand code as
Complexity
understand compare to Programming language
Program having a set of instructions Based on an idea of Scripts written to
Formation
converted to executables control another program
Run independently of an exterior
Running Run inside another program
program
5
Department of Computer Engineering www.sandipfoundation.org
Server#side Programming 04

It is the program that runs on server dealing with the generation of content of web page.
1) Querying the database
2) Operations over databases
3) Access/Write a file on server.
4) Interact with other servers.
5) Structure web applications.
6) Process user input. For example if user input is a text in search box, run a search algorithm on
data stored on server and send the results.

Examples :
The Programming languages for server-side programming are :
1) PHP
2) C++
3) Java and JSP
4) Python
5) Ruby on Rails
6
Department of Computer Engineering www.sandipfoundation.org
Client#side Programming 04

• It is the program that runs on the client machine (browser) and deals with the user
interface/display and any other processing that can happen on client machine like
reading/writing cookies.
1) Interact with temporary storage
2) Make interactive web pages
3) Interact with local storage
4) Sending request for data to server
5) Send request to server
6) work as an interface between server and user

The Programming languages for client-side programming are :


1) Javascript
2) VBScript
3) HTML
4) CSS
5) AJAX
7
Department of Computer Engineering www.sandipfoundation.org
Server#side Scripting Vs Client#side Scripting 04

BASIS FOR
SERVER-SIDE SCRIPTING CLIENT-SIDE SCRIPTING
COMPARISON
Works in the back end which could not Works at the front end and script
Basic
be visible at the client end. are visible among the users.
Does not need interaction with the
Processing Requires server interaction.
server.
PHP, ASP.net, Ruby on Rails,
Languages involved HTML, CSS, JavaScript, etc.
ColdFusion, Python, etcetera.

Could effectively customize the web


Affect Can reduce the load to the server.
pages and provide dynamic websites.

Security Relatively secure. Insecure


8
Department of Computer Engineering www.sandipfoundation.org
Introduction 04

• For a Web page, HTML supplies document content and structure while CSS provides
presentation styling
• In addition, client-side scripts can control browser actions associated with a Web page.
• Client-side scripts are almost written in the Javascript language to control browser’s
actions.
• Client-side scripting can make Web pages more dynamic and more responsive

9
Department of Computer Engineering www.sandipfoundation.org
Tasks performed by client#side scripts 04

• Checking correctness of user input


• Monitoring user events and specifying reactions
• Replacing and updating parts of a page
• Changing the style and position of displayed elements dynamically
• Modifying a page in response to events
• Getting browser information
• Making the Web page different depending on the browser and browser features
• Generating HTML code for parts of the page

10
Department of Computer Engineering www.sandipfoundation.org
Pros & Cons of Client Side Scripting 04

Pros
• Allow for more interactivity by immediately responding to users’ actions.
• Execute quickly because they do not require a trip to the server.
• The web browser uses its own resources, and eases the burden on the server.
• It saves network bandwidth.

Cons
• Code is loaded in the browser so it will be visible to the client.
• Code is modifiable.
• Local files and databases cannot be accessed.
• User is able to disable client side scripting

11
Department of Computer Engineering www.sandipfoundation.org
Overview of JavaScript 04

• It is dynamic Computer Programming language


• Lightweight and most commonly used for web pages.
• It interact with user and make a web page dynamic.
• Using HTML only static pages can be created.
• No user interaction with HTML
• JavaScript was developed by Netscape in 1995.
• Its initial name was LiveScript.
• Later on Sun Microsystems joined Netscape and the name is changed to JavaScript.

12
Department of Computer Engineering www.sandipfoundation.org
Features of JavaScript 04

13
Department of Computer Engineering www.sandipfoundation.org
How to write JavaScript Document? 04

• JavaScript can be implemented using JavaScript statements that are placed within the <script>...
</script> HTML tags in a web page.
• You can place the <script> tags, containing your JavaScript, anywhere within your web page, but it is
normally recommended that you should keep it within the <head> tags.
• The <script> tag alerts the browser program to start interpreting all the text between these tags as a
script.
• A simple syntax of your JavaScript will appear as follows.
<script ...>
JavaScript code
</script>
• The script tag takes two important a5ributes −
• Language − This aHribute specifies what scripJng language you are using.
• Type − This aHribute is what is now recommended to indicate the scripJng language in use and its
value should be set to "text/javascript".
• So your JavaScript segment will look like −
<script language = "javascript" type = "text/javascript">
JavaScript code
</script>
14
Department of Computer Engineering www.sandipfoundation.org
Your First JavaScript Code 04

<html>
<body>
<script language = "javascript" type = "text/javascript">
<!--
document.write("Hello World!")
//-->
</script>
</body>
</html>

• The comment ends with a "//-->".


• Here "//" signifies a comment in JavaScript, so we add that to prevent a browser from
reading the end of the HTML comment as a piece of JavaScript code.
• Next, we call a function document.write which writes a string into our HTML
document.

15
Department of Computer Engineering www.sandipfoundation.org
Enabling JavaScript in Browsers 04

• JavaScript in Firefox
• Here are the steps to turn on or turn off JavaScript in Firefox −
• Open a new tab → type about: config in the address bar.
• Then you will find the warning dialog. Select I’ll be careful, I promise!
• Then you will find the list of configure options in the browser.
• In the search bar, type javascript.enabled.
• There you will find the option to enable or disable javascript by right-clicking on the value of that
opJon → select toggle.
• JavaScript in Chrome
• Here are the steps to turn on or turn off JavaScript in Chrome −
• Click the Chrome menu at the top right hand corner of your browser.
• Select Settings.
• Click Show advanced settings at the end of the page.
• Under the Privacy section, click the Content settings button.
• In the "Javascript" section, select "Do not allow any site to run JavaScript" or "Allow all sites to
run JavaScript (recommended)“.
16
Department of Computer Engineering www.sandipfoundation.org
JavaScript Example 04

• 3 Places to put JavaScript code


1. Between the body tag of html
2. Between the head tag of html
3. In .js file (external javaScript)

1) JavaScript Example : code between the body tag


• In the above example, we have displayed the dynamic content using JavaScript. Let’s see the
simple example of JavaScript that displays alert dialog box.

<html>
<body>
<script type="text/javascript">
alert("Hello Javatpoint");
</script>
</body>
</html>
17
Department of Computer Engineering www.sandipfoundation.org
JavaScript Example 04

2) JavaScript Example : code between the head tag


• Let’s see the same example of displaying alert dialog box of JavaScript that is contained inside the head tag.
• In this example, we are creating a function msg().
• To call function, you need to work on event. Here we are using onclick event to call msg() function.
<html>
<head>
<script type="text/javascript">
function msg()
{
alert("Hello Javatpoint");
}
</script>
</head>
<body>
<p>Welcome to Javascript</p>
<form>
<input type="button" value="click" onclick="msg()"/>
</form>
</body>
</html>
18
Department of Computer Engineering www.sandipfoundation.org
JavaScript Example 04

• JavaScript in External File


• We can create external JavaScript file and embed it in many html page.
• It provides code re usability because single JavaScript file can be used in several html pages.
• 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.
• Let's create an external JavaScript file that prints Hello Javatpoint in a alert dialog box.

message.js
function msg()
{
alert("Hello Javatpoint");
}

19
Department of Computer Engineering www.sandipfoundation.org
JavaScript Example 04

• Let's include the JavaScript file into html page. It calls the JavaScript function on button click.
• index.html

<head>
<script type="text/javascript" src="message.js">
</script>
</head>
<body>
<p>Welcome to JavaScript</p>
<form>
<input type="button" value="click" onclick="msg()"/>
</form>
</body>
</html>

20
Department of Computer Engineering www.sandipfoundation.org
Object Name, Property, Method, Dot Syntax, Main Event 04

Obect Properties:
Object Name:
• Object is entity. In JavaScript document, window, forms, fields, buttons are some properly used
objects.
• Each object is identified by ID or name.
• Array of objects or Array of collections can also be created.
Property:
• It is the value associated with each object.
• Each object has its own set of properties.
• For example, a form object has a title, a width, and a height—to mention a few properties
Method:
• A method is a process performed by an object when it receives a message.
• For example, a Submit button on a form is an object. Its Submit label and the dimensions of the
button are properties of the button object.
• If you click the Submit button, the form is submitted to the server-side application. In other
words, clicking the Submit button causes the button to process a method.
21
Department of Computer Engineering www.sandipfoundation.org
Object Name, Property, Method, Dot Syntax, Main Event 04

Dot Syntax:
• Used to access properties and methods of object.
• You access an object’s properties and methods by using the dot syntax along with the
object name and its property or method.
• So, for example, here’s how you would
• identify the background color of a document and the write method for a document:
document.bgColor
document.write()

Main Event:
• Event causes JavaScript to execute the code.
• In JavaScript when user submits form, clicks button, writes something to text box the
corresponding event gets triggered and execution of appropriate code is done through
Event Handling.

22
Department of Computer Engineering www.sandipfoundation.org
Values and Variables 04

Values:
It uses six types of values:
• Number: It is numeric value can be integer or float.
• String: It is a collection of characters. Enclosed within single or double quote.
• Boolean: It contains true and false values. Values can be compared with variables and can be
used in assignment statement.
• Null: This value can be assigned by reserved word ‘null’. It means no value. If we try to access
null value error will occur.
• Object: It is the entity that represents some value. Ex: Form is an object on which some
components can be placed and used.
• Function: It is intended for execution of some task. There are predefined and user defined
functions. The ‘function’ keyword is used to define function.

23
Department of Computer Engineering www.sandipfoundation.org
Values and Variables 04

• Variable
• A statement that tells the browser to create a variable requires two parts:
• The special word, called a keyword, tells the browser that you want it to create a variable.
• The word you need to use to declare a variable is var.
• The variable name can consist of any letter, digit, and an underscore, but it cannot begin with a
digit.
<script type="text/javascript">
<!--
var money;
var name;

You can also declare multiple variables with the same var keyword as follows:
<script type="text/javascript">
<!--
var money, name;
//-->
</script>
24
Department of Computer Engineering www.sandipfoundation.org
Values and Variables 04

• Storing a value in a variable is called variable initialization. You can do variable initialization at
the time of variable creation or at a later point in time when you need that variable.
• For instance, you might create a variable named money and assign the value 2000.50 to it later.
For another variable, you can assign a value at the time of initialization as follows.
<script type="text/javascript">
<!--
var name = "Ali";
var money;
money = 2000.50;
//-->
</script>

25
Department of Computer Engineering www.sandipfoundation.org
Values and Variables 04

<html>
<head>
<title>Variables in Javascript</title>
</head>
<body>
<script type="text/javascript">
//Variables in JavaScript
var a,b,c;
var string;
a=2;
b=3;
c=a+b;
string="The result is :";
document.write("Performing addition of a and b"+"</br>");
document.write(string);
document.write(c);
</script>
</center>
</body>
</html> 26
Department of Computer Engineering www.sandipfoundation.org
04
Keywords
• These are the reserved words having some special meaning associated with it.

abstract else instanceof super


boolean enum int switch
break export interface synchronized
byte extends let this
case false long throw
catch final native throws
char finally new transient
class float null true
const for package try
continue function private typeof
debugger goto protected var
default if public void
delete implements return volatile
do import short while
double in static with

27
Department of Computer Engineering www.sandipfoundation.org
Operators and Expressions 04

Sr. No. Type Operato Meaning Example


r
1. Arithmetic + Addition or Unary c=a+b
plus
- Subtraction or Unary d=-a
minus
* Multiplication c=a*b
/ Division c=a/b
% Mod c=a%b
2. Logical && AND Operator 0&&1
|| OR Operator 0||1
3. Increment ++ Increment by one ++i or i++
4. Decrement -- Decrement by one --i or i--
5. Conditional ?: Conditional Operator a>b?true:false
28
Department of Computer Engineering www.sandipfoundation.org
Operators and Expressions 04

Sr. No. Type Operator Meaning Example


6. Assignment = Is assigned to a=5
+= Add a value then assign a+=5 (a=a+5)
-= Subtract a value then assign a-=5(a=a-5)
*= Multiply a value then assign a*=5(a=a*5)
/= Divide a value then assign a/=5(a=a/5)
7. Relational < Less than a<4
> Greater than a>4
<= Less than equal to a<=4
>= Greater than equal to a>=4
== Equal to a==4
!= Not Equal to a!=4
=== equal value and equal type a===5
!== not equal value or not equal type a!==5
29
Department of Computer Engineering www.sandipfoundation.org
Operators and Expressions – Arithmetic Operators 04

<html> document.write("a + b + c = ");


<body> result = a + b + c;
<script type = "text/javascript"> document.write(result);
<!-- document.write(linebreak);
var a = 33; a = ++a;
var b = 10; document.write("++a = ");
var c = "Test"; result = ++a;
var linebreak = "<br />"; document.write(result);
document.write("a + b = "); document.write(linebreak);
result = a + b; b = --b;
document.write(result); document.write("--b = ");
document.write(linebreak); result = --b;
document.write("a - b = "); document.write(result);
result = a - b; document.write(linebreak);
document.write(result); //-->
document.write(linebreak); </script>
document.write("a / b = "); </body>
result = a / b; </html>
document.write(result); Output :
document.write(linebreak); a + b = 43 a - b = 23 a / b = 3.3
document.write("a % b = "); a%b=3 a + b + c = 43 Test
result = a % b;
++a = 35 --b = 8
document.write(result);
document.write(linebreak); 30
Department of Computer Engineering www.sandipfoundation.org
Operators and Expressions – Comparison Operators 04

<html> document.write(result);
<body> document.write(linebreak);
<script type = "text/javascript">
var a = 10; document.write("(a >= b) => ");
var b = 20; result = (a >= b);
var linebreak = "<br />"; document.write(result);
document.write(linebreak);
document.write("(a == b) => ");
result = (a == b); document.write("(a <= b) => ");
document.write(result); result = (a <= b);
document.write(linebreak); document.write(result);
document.write(linebreak);
document.write("(a < b) => "); </script>
result = (a < b); </body>
document.write(result); </html>
document.write(linebreak);
Output
document.write("(a > b) => "); (a == b) => false
result = (a > b); (a < b) => true
document.write(result); (a > b) => false
document.write(linebreak); (a != b) => true
(a >= b) => false
document.write("(a != b) => "); a <= b) => true
result = (a != b); 31
Department of Computer Engineering www.sandipfoundation.org
Operators and Expressions – Logical Operators 04

<html>
<body> document.write("!(a && b) => ");
<script type = "text/javascript"> result = (!(a && b));
<!-- document.write(result);
var a = true; document.write(linebreak);
var b = false; //-->
var linebreak = "<br />"; </script>
<p>Set the variables to different values
document.write("(a && b) => "); and different operators and then try...</p>
result = (a && b); </body>
document.write(result); </html>
document.write(linebreak);
Output
document.write("(a || b) => "); (a && b) => false
result = (a || b); (a || b) => true
document.write(result); !(a && b) => true
document.write(linebreak);

32
Department of Computer Engineering www.sandipfoundation.org
Operators and Expressions – Bitwise Operators 04

<html> document.write(linebreak);
<body> document.write("(a << b) => ");
<script type = "text/javascript"> result = (a << b);
var a = 2; // Bit presentation 10 document.write(result);
var b = 3; // Bit presentation 11 document.write(linebreak);
var linebreak = "<br />"; document.write("(a >> b) => ");
document.write("(a & b) => "); result = (a >> b);
result = (a & b); document.write(result);
document.write(result); document.write(linebreak);
document.write(linebreak); </script>
document.write("(a | b) => "); </body>
result = (a | b); </html>
document.write(result);
document.write(linebreak); Output:
document.write("(a ^ b) => "); (a & b) => 2
result = (a ^ b); (a | b) => 3
document.write(result); (a ^ b) => 1
document.write(linebreak); (~b) => -4
document.write("(~b) => "); (a << b) => 16
result = (~b); (a >> b) => 0
document.write(result);
33
Department of Computer Engineering www.sandipfoundation.org
Operators and Expressions – Comparison Operators 04

<html> document.write(result);
<body> document.write(linebreak);
<script type = "text/javascript">
var a = 33; document.write("Value of a => (a /= b) => ");
var b = 10; result = (a /= b);
var linebreak = "<br />"; document.write(result);
document.write("Value of a => (a = b) => "); document.write(linebreak);
result = (a = b);
document.write(result); document.write("Value of a => (a %= b) => ");
document.write(linebreak); result = (a %= b);
document.write(result);
document.write("Value of a => (a += b) => "); document.write(linebreak);
result = (a += b); </script>
document.write(result); </body>
document.write(linebreak); </html>

document.write("Value of a => (a -= b) => "); Output


result = (a -= b); Value of a => (a = b) => 10
document.write(result); Value of a => (a += b) => 20
document.write(linebreak); Value of a => (a -= b) => 10
Value of a => (a *= b) => 100
document.write("Value of a => (a *= b) => "); Value of a => (a /= b) => 10
result = (a *= b); Value of a => (a %= b) => 0
34
Department of Computer Engineering www.sandipfoundation.org
Operators and Expressions – Conditional Operator (? :) 04

• The conditional operator first evaluates an expression for a true or false value and then executes one of the two given
statements depending upon the result of the evaluation.
<html>
<body>
<script type = "text/javascript">
var a = 10;
var b = 20;
var linebreak = "<br />";
document.write ("((a > b) ? 100 : 200) => ");
result = (a > b) ? 100 : 200;
document.write(result);
document.write(linebreak);
document.write ("((a < b) ? 100 : 200) => ");
result = (a < b) ? 100 : 200;
document.write(result);
document.write(linebreak);
</script>
</body>
</html>
Output
((a > b) ? 100 : 200) => 200
((a < b) ? 100 : 200) => 100
35
Department of Computer Engineering www.sandipfoundation.org
If Statement, if…else, if…elseif, nested if statement 04

• If statement }
• It evaluates the content only if expression is true. The • If...else if statement
signature of JavaScript if statement is given below. • It evaluates the content only if expression is true from
if(expression) several expressions. The signature of JavaScript if else if
{ statement is given below.
if(expression1)
//content to be evaluated {
} //content to be evaluated if expression1 is true
}
• If...else Statement else if(expression2)
• It evaluates the content whether condition is true of false. {
//content to be evaluated if expression2 is true
The syntax of JavaScript if-else statement is given below. }
if(expression) else if(expression3)
{ {
//content to be evaluated if condition is //content to be evaluated if expression3 is true
true }
Else
} {
Else //content to be evaluated if no expression is true
{
//content to be evaluated if condition is }
false
36
Department of Computer Engineering www.sandipfoundation.org
If Statement 04

<html>
<body>
<script type = "text/javascript">
<!--
var age = 20;
if( age > 18 )
{
document.write("<b>Qualifies for driving</b>");
}
//-->
</script>
<p>Set the variable to different value and then try...</p>
</body>
</html>

37
Department of Computer Engineering www.sandipfoundation.org
if…else statement 04

<html>
<body>
<script type = "text/javascript">
<!--
var age = 15;

if( age > 18 ) {


document.write("<b>Qualifies for driving</b>");
} else {
document.write("<b>Does not qualify for driving</b>");
}
//-->
</script>
<p>Set the variable to different value and then try...</p>
</body>
</html>

38
Department of Computer Engineering www.sandipfoundation.org
if…else if.. statement 04

<html>
<body>
<script type = "text/javascript">
<!--
var book = "maths";
if( book == "history" ) {
document.write("<b>History Book</b>");
} else if( book == "maths" ) {
document.write("<b>Maths Book</b>");
} else if( book == "economics" ) {
document.write("<b>Economics Book</b>");
} else {
document.write("<b>Unknown Book</b>");
}
//-->
</script>
<p>Set the variable to different value and then try...</p>
</body>
<html>

39
Department of Computer Engineering www.sandipfoundation.org
Switch…case statement 04

• The objective of a switch statement is to give an expression to evaluate and several different statements
to execute based on the value of the expression.
• The interpreter checks each case against the value of the expression until a match is found. If nothing
matches, a default condition will be used.

switch (expression) {
case condition 1: statement(s)
break;

case condition 2: statement(s)


break;
...

case condition n: statement(s)


break;

default: statement(s)
}

40
Department of Computer Engineering www.sandipfoundation.org
Switch…case statement 04

<html>
<body>
<script type = "text/javascript">
var grade = 'A';
document.write("Entering switch block<br />");
switch (grade) {
case 'A': document.write("Good job<br />");
break;
case 'B': document.write("Pretty good<br />");
break;
case 'C': document.write("Passed<br />");
break;
case 'D': document.write("Not so good<br />");
break;
case 'F': document.write("Failed<br />");
break;
default: document.write("Unknown grade<br />")
}
document.write("Exiting switch block");
</script>
</body>
41
</html> Department of Computer Engineering www.sandipfoundation.org
Loop statement – for loop 04

• The JavaScript loops are used to iterate the piece • The syntax of for loop is given below.
of code using for, while, do while or for-in loops. for (initialization; condition; increment)
• It makes the code compact. It is mostly used in {
array. code to be executed
}
There are four types of loops in JavaScript.
1.for loop <html>
2.while loop <body>
3.do-while loop <script>
4.for-in loop for (i=1; i<=5; i++)
{
document.write(i + "<br/>")
1) JavaScript For loop }
• The JavaScript for loop iterates the elements for </script>
the fixed number of times. </body>
• It should be used if number of iteration is </html>
known.
Department of Computer Engineering www.sandipfoundation.org
Loop statement – while loop 04

• JavaScript while loop <html>


• The JavaScript while loop iterates the elements <body>
for the infinite number of times. <script>
• It should be used if number of iteration is not var i=11;
known. The syntax of while loop is given below. while (i<=15)
{
while (condition) document.write(i + "<br/>");
{ i++;
code to be executed }
} </script>
</body>
</html>

43
Department of Computer Engineering www.sandipfoundation.org
Loop statement – do…while loop 04

• JavaScript do while loop <html>


• The JavaScript do while loop iterates the <body>
elements for the infinite number of times like <script>
while loop. var i=21;
• But, code is executed at least once whether do
condition is true or false. {
• The syntax of do while loop is given below. document.write(i + "<br/>");
i++;
}
Do
while (i<=25);
{
</script>
code to be executed
</body>
}while (condition);
</html>

44
Department of Computer Engineering www.sandipfoundation.org
Loop statement – Break statement 04

• The break statement can also be used to jump out of a loop.


• The break statement breaks the loop and continues executing the code after the loop
(if any):

Syntax:
jump-statement;
break;

Ex:
for (i = 0; i < 10; i++)
{
if (i === 3)
{ break; }
text += "The number is " + i + "<br>";
}
45
Department of Computer Engineering www.sandipfoundation.org
Loop statement – Continue statement 04

• The continue statement breaks one iteration (in the loop), if a specified condition
occurs, and continues with the next iteration in the loop.

• Syntax:
jump-statement;
break;

Ex:
for (i = 0; i < 10; i++)
{
if (i ==3)
{ continue; }
text += "The number is " + i + "<br>";
}

46
Department of Computer Engineering www.sandipfoundation.org
19

Presented By
Prof. Vishal B. Ohol
Experience : 10 Years
ME(Software Engg)

For any queries!!!


9975641048

[email protected]

47
Department of Computer Engineering www.sandipfoundation.org

You might also like