SlideShare a Scribd company logo
LOOPS AND ARRAYS
SESSION 14
Aptech Computer Education
Presented by Muhammad Ehtisham Siddiqui (BSCS)
1
Objectives
 In this Session, you will learn to:
 Explain while loop
 Explain for loop
 Explain do while loop
 Explain break and continue statement
 Explain single-dimensional arrays
 Explain multi-dimensional arrays
 Explain for .. in loop
Presented by Muhammad Ehtisham Siddiqui (BSCS)
2
Loop
Presented by Muhammad Ehtisham Siddiqui
(BSCS)
3
 Loops in JavaScript are used to execute the same block of code a
specified number of times or while a specified condition is true.
 Very often when you write code, you want the same block of code to run
over and over again in a row. Instead of adding several almost equal
lines in a script we can use loops to perform a task like this.
Types of Loops
Presented by Muhammad Ehtisham Siddiqui
(BSCS)
4
 For Loop
 - loops through a block of code a number of times
 While Loop
 loops through a block of code while a specified condition is true
 do/while Loop
 also loops through a block of code while a specified condition is true
 for/in Loop
 loops through the properties of an object
For Loops
Presented by Muhammad Ehtisham Siddiqui
(BSCS)
5
 The 'for' loop is the most compact form of looping. It includes the following three
important parts
 The loop initialization where we initialize our counter to a starting value. The
initialization statement is executed before the loop begins.
 The test statement which will test if a given condition is true or not. If the condition is
true, then the code given inside the loop will be executed, otherwise the control will
come out of the loop.
 The iteration statement where you can increase or decrease your counter.
 Syntax:-
for (initialization; test condition; iteration statement)
{
Statement(s) to be executed if test condition is true
}
Sample Code
Presented by Muhammad Ehtisham Siddiqui
(BSCS)
6
<html>
<body>
<script type="text/javascript">
var count;
document.write("Starting Loop" + "<br />");
for(count = 0; count < 10; count++){
document.write("Current Count : " + count );
document.write("<br />");
}
document.write("Loop stopped!");
</script>
<p>Set the variable to different value and then try...</p>
</body>
</html>
OUTPUT:
Starting Loop
Current Count : 0
Current Count : 1
Current Count : 2
Current Count : 3
Current Count : 4
Current Count : 5
Current Count : 6
Current Count : 7
Current Count : 8
Current Count : 9
Loop stopped!
Set the variable to different
value and then try...
Sample Code(Try It)
Presented by Muhammad Ehtisham Siddiqui
(BSCS)
7
<html>
<body>
<script type="text/javascript">
var count;
var table=2;
document.write("Starting Loop" + "<br />");
for(count = 1; count <= 10; count++){
document.write(table+" X "+ count+" = "+table*count );
document.write("<br />");
}
document.write("Loop stopped!");
</script>
</body>
</html>
Output:
Starting Loop
2 X 1 = 2
2 X 2 = 4
2 X 3 = 6
2 X 4 = 8
2 X 5 = 10
2 X 6 = 12
2 X 7 = 14
2 X 8 = 16
2 X 9 = 18
2 X 10 = 20
Loop stopped!
While loop
Presented by Muhammad Ehtisham Siddiqui
(BSCS)
8
 The while statement creates a loop that is executed while a specified
condition is true.
 The loop will continue to run as long as the condition is true. It will only stop
when the condition becomes false.
 Syntax:
while (Condition)
{
Statement(s) to be executed if expression is true
}
Example Code (Try It)
Presented by Muhammad Ehtisham Siddiqui
(BSCS)
9
<html>
<head> <script type="text/javascript">
<!--
var count = 0;
document.write("Starting Loop ");
while (count < 10){
document.write("Current Count : " + count + "<br />");
count++;
}
document.write("Loop stopped!");
//-->
</script></head>
<body>
<p>Set the variable to different value and then try...</p>
</body>
</html>
OUTPUT:
Starting Loop
Current Count : 0
Current Count : 1
Current Count : 2
Current Count : 3
Current Count : 4
Current Count : 5
Current Count : 6
Current Count : 7
Current Count : 8
Current Count : 9
Loop stopped!
Set the variable to different
value and then try...
Example Code (Try It)
Presented by Muhammad Ehtisham Siddiqui
(BSCS)
10
<html>
<head> <script type="text/javascript">
<!--
var count = 1;
document.write("Starting Loop <br/> ");
while (count < 10){
document.write(5+ " X " + count + " = "+count*5+" <br />");
count++;
}
document.write("Loop stopped!");
//-->
</script></head>
<body>
<p>Set the variable to different value and then try...</p>
</body>
</html>
Output:
Starting Loop
2 X 1 = 2
2 X 2 = 4
2 X 3 = 6
2 X 4 = 8
2 X 5 = 10
2 X 6 = 12
2 X 7 = 14
2 X 8 = 16
2 X 9 = 18
2 X 10 = 20
Loop stopped!
Do While Loop
Presented by Muhammad Ehtisham Siddiqui
(BSCS)
11
 The do...while loop is similar to the while loop except that the condition check
happens at the end of the
 This means that the loop will always be executed at least once, even if the
condition is false.
 Syntax:
do{
Statement(s) to be executed;
} while (expression);
Code
Presented by Muhammad Ehtisham Siddiqui
(BSCS)
12
<html>
<body>
<script type="text/javascript">
<!--
var count = 0;
document.write("Starting Loop" + "<br />");
do{
document.write("Current Count : " + count + "<br />");
count++;
}
while (count < 5);
document.write ("Loop stopped!");
//-->
</script>
<p>Set the variable to different value and then try...</p>
</body>
</html>
OUTPUT
Starting Loop
Current Count : 0
Current Count : 1
Current Count : 2
Current Count : 3
Current Count : 4
Loop stopped!Set the variable to
different value and then try...
Code
Presented by Muhammad Ehtisham Siddiqui
(BSCS)
13
<html>
<body>
<script type="text/javascript">
<!--
var count = 1;
document.write("Starting Loop" + "<br />");
do{
document.write("6 * " + count +" = "+count*6+ "<br />");
count++;
}
while (count <=10);
document.write ("Loop stopped!");
//-->
</script>
<p>Set the variable to different value and then try...</p>
</body>
</html>
Starting Loop
6 * 1 = 6
6 * 2 = 12
6 * 3 = 18
6 * 4 = 24
6 * 5 = 30
6 * 6 = 36
6 * 7 = 42
6 * 8 = 48
6 * 9 = 54
6 * 10 = 60
Loop stopped!
Set the variable to different value
and then try...
For in Loop
Presented by Muhammad Ehtisham Siddiqui
(BSCS)
14
 The for...in loop is used to loop through an object's properties.
 In each iteration, one property from object is assigned to variablename and
this loop continues till all the properties of the object are exhausted.
 Syntax:
for (variablename in object){
statement or block to execute
}
Code
Presented by Muhammad Ehtisham Siddiqui
(BSCS)
15
<!DOCTYPE html>
<html>
<body>
<p>Click the button to loop through the properties of an object.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var person = {fname:"John", lname:"Doe", age:25};
var text = "";
var x;
for (x in person) {
text += person[x] + " ";
} document.getElementById("demo").innerHTML = text; }
</script>
</body>
</html>
Array
Presented by Muhammad Ehtisham Siddiqui
(BSCS)
16
 JavaScript arrays are used to store multiple values in a single
variable.
 An array is a special variable, which can hold more than one value
at a time.
 If you have a list of items (a list of car names, for example), storing
the cars in single variables could look like this:
var car1 = "Saab";
var car2 = "Volvo";
var car3 = "BMW";
 owever, what if you want to loop through the cars and find a specific
one? And what if you had not 3 cars, but 300?
 The solution is an array!
var cars = ["Saab", "Volvo", "BMW"];
Creating an Array
Presented by Muhammad Ehtisham Siddiqui
(BSCS)
17
 Using an array literal is the easiest way to create a JavaScript Array.
 Syntax:
var array_name = [item1, item2, ...];
 Spaces and line breaks are not important. A declaration can span
multiple lines:
var cars = [
"Saab",
"Volvo",
"BMW"
];
 The following example also creates an Array, and assigns values to
it:
var cars = new Array("Saab", "Volvo", "BMW");
Example
Presented by Muhammad Ehtisham Siddiqui
(BSCS)
18
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Arrays</h2>
<p id="demo"></p>
<script>
var person = [];
person[0] = "John";
person[1] = "Doe";
person[2] = 46;
document.getElementById("demo").innerHTML =
person[0] + " " + person.length;
</script>
</body>
</html>
Example
Presented by Muhammad Ehtisham Siddiqui
(BSCS)
19
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Arrays</h2>
<p>Avoid using new Array(). Use [] instead.</p>
<p id="demo"></p>
<script>
//var points = new Array(40, 100, 1, 5, 25, 10);
var points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = points[0];
</script>
</body>
</html>
Example(sort)
Presented by Muhammad Ehtisham Siddiqui
(BSCS)
20
<!DOCTYPE html>
<html><body>
<h2>JavaScript Array Sort</h2>
<p>The sort() method sorts an array alphabetically.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script> var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;
function myFunction() {
fruits.sort();
document.getElementById("demo").innerHTML = fruits;
}
</script>
</body>
</html>
Example(reverse)
Presented by Muhammad Ehtisham Siddiqui
(BSCS)
21
<!DOCTYPE html><html><body>
<h2>JavaScript Array Sort</h2>
<p>The sort() method sorts an array alphabetically.</p>
<p>The reverse() method reverts the elements.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;
function myFunction() {
fruits.sort();
fruits.reverse();
document.getElementById("demo").innerHTML = fruits;
}
</script></body></html>
2 Dimensional Array
Presented by Muhammad Ehtisham Siddiqui (BSCS)
22
 var arr = [[],[]]; // 2 dimensional array
 To enter data in two dimentional array you can
use
 arr[0][2] = 'Hi Mr.A';
 arr[1][3] = 'Hi Mr.B';
 To Read tw dimenstional array
 alert(arr[0][2]);
 alert(arr[1][3]);
Questions?
Presented by Muhammad Ehtisham Siddiqui (BSCS)
23

More Related Content

PDF
Fun Teaching MongoDB New Tricks
PPTX
Mongoose and MongoDB 101
PDF
Streams in Node.js
PPT
JavaScript Basics with baby steps
PPT
Test innode
PDF
Intro to Sail.js
PDF
A XSSmas carol
PPTX
Node.js System: The Landing
Fun Teaching MongoDB New Tricks
Mongoose and MongoDB 101
Streams in Node.js
JavaScript Basics with baby steps
Test innode
Intro to Sail.js
A XSSmas carol
Node.js System: The Landing

What's hot (9)

KEY
Node.js 0.8 features
PPTX
hacking with node.JS
PPTX
Choosing the right parallel compute architecture
PDF
Locking the Throneroom 2.0
PDF
Security Challenges in Node.js
PDF
ORM2Pwn: Exploiting injections in Hibernate ORM
PDF
Node.js and How JavaScript is Changing Server Programming
PDF
Shared memory and multithreading in Node.js - Timur Shemsedinov - JSFest'19
PDF
DEF CON 23 - amit ashbel and maty siman - game of hacks
Node.js 0.8 features
hacking with node.JS
Choosing the right parallel compute architecture
Locking the Throneroom 2.0
Security Challenges in Node.js
ORM2Pwn: Exploiting injections in Hibernate ORM
Node.js and How JavaScript is Changing Server Programming
Shared memory and multithreading in Node.js - Timur Shemsedinov - JSFest'19
DEF CON 23 - amit ashbel and maty siman - game of hacks
Ad

Similar to JavaScript Session 3 (20)

PPTX
10. session 10 loops and arrays
PPTX
Loops in java script
DOCX
Loops and iteration.docx
PDF
1660213363910.pdf
PPTX
Loops (Refined).pptx
PDF
JavaScript Looping Statements
PPT
javascript arrays in details for udergaduate studenets .ppt
PPS
CS101- Introduction to Computing- Lecture 26
PPT
POLITEKNIK MALAYSIA
PPTX
First javascript workshop : first basics
PDF
Fewd week5 slides
PPTX
Java Script Basic to Advanced For Beginner to Advanced Learner.pptx
PPTX
FFW Gabrovo PMG - JavaScript 2
DOC
Web programming[10]
PPT
CSIS 138 JavaScript Class3
PPTX
JS Control Statements and Functions.pptx
DOCX
Janakiram web
PDF
Loops in JavaScript
PDF
Handout - Introduction to Programming
PPTX
Javascript 101
10. session 10 loops and arrays
Loops in java script
Loops and iteration.docx
1660213363910.pdf
Loops (Refined).pptx
JavaScript Looping Statements
javascript arrays in details for udergaduate studenets .ppt
CS101- Introduction to Computing- Lecture 26
POLITEKNIK MALAYSIA
First javascript workshop : first basics
Fewd week5 slides
Java Script Basic to Advanced For Beginner to Advanced Learner.pptx
FFW Gabrovo PMG - JavaScript 2
Web programming[10]
CSIS 138 JavaScript Class3
JS Control Statements and Functions.pptx
Janakiram web
Loops in JavaScript
Handout - Introduction to Programming
Javascript 101
Ad

More from Muhammad Ehtisham Siddiqui (20)

PPTX
C programming Tutorial Session 4
PPTX
C programming Tutorial Session 4
PPTX
C programming Tutorial Session 3
PPTX
C programming Tutorial Session 2
PPTX
C programming Tutorial Session 1
PPTX
HTML5 Web storage
PPTX
J Query (Complete Course) by Muhammad Ehtisham Siddiqui
PPTX
JavaScript Session 2
PPTX
Javascript session 1
PPTX
Html audio video
PPTX
Html 5 geolocation api
PPTX
Buiding Next Generation Websites Session 8 by Muhammad Ehtisham Siddiqui
PPTX
Building Next Generation Websites Session 7 by Muhammad Ehtisham Siddiqui
PPTX
Building Next Generation Websites Session 6 by Muhammad Ehtisham Siddiqui
PPTX
Building Next Generation Websites by Muhammad Ehtisham Siddiqui
PPTX
Building Next Generation Websites Session6
PPTX
Building Next Generation Websites Session5
PPTX
Building Next Generation Websites Session4
PPTX
Office session14
C programming Tutorial Session 4
C programming Tutorial Session 4
C programming Tutorial Session 3
C programming Tutorial Session 2
C programming Tutorial Session 1
HTML5 Web storage
J Query (Complete Course) by Muhammad Ehtisham Siddiqui
JavaScript Session 2
Javascript session 1
Html audio video
Html 5 geolocation api
Buiding Next Generation Websites Session 8 by Muhammad Ehtisham Siddiqui
Building Next Generation Websites Session 7 by Muhammad Ehtisham Siddiqui
Building Next Generation Websites Session 6 by Muhammad Ehtisham Siddiqui
Building Next Generation Websites by Muhammad Ehtisham Siddiqui
Building Next Generation Websites Session6
Building Next Generation Websites Session5
Building Next Generation Websites Session4
Office session14

Recently uploaded (20)

PDF
TR - Agricultural Crops Production NC III.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
GDM (1) (1).pptx small presentation for students
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Institutional Correction lecture only . . .
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Lesson notes of climatology university.
PPTX
Pharma ospi slides which help in ospi learning
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
TR - Agricultural Crops Production NC III.pdf
RMMM.pdf make it easy to upload and study
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Sports Quiz easy sports quiz sports quiz
GDM (1) (1).pptx small presentation for students
O7-L3 Supply Chain Operations - ICLT Program
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Institutional Correction lecture only . . .
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Lesson notes of climatology university.
Pharma ospi slides which help in ospi learning
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPH.pptx obstetrics and gynecology in nursing
Supply Chain Operations Speaking Notes -ICLT Program
Computing-Curriculum for Schools in Ghana
Pharmacology of Heart Failure /Pharmacotherapy of CHF

JavaScript Session 3

  • 1. LOOPS AND ARRAYS SESSION 14 Aptech Computer Education Presented by Muhammad Ehtisham Siddiqui (BSCS) 1
  • 2. Objectives  In this Session, you will learn to:  Explain while loop  Explain for loop  Explain do while loop  Explain break and continue statement  Explain single-dimensional arrays  Explain multi-dimensional arrays  Explain for .. in loop Presented by Muhammad Ehtisham Siddiqui (BSCS) 2
  • 3. Loop Presented by Muhammad Ehtisham Siddiqui (BSCS) 3  Loops in JavaScript are used to execute the same block of code a specified number of times or while a specified condition is true.  Very often when you write code, you want the same block of code to run over and over again in a row. Instead of adding several almost equal lines in a script we can use loops to perform a task like this.
  • 4. Types of Loops Presented by Muhammad Ehtisham Siddiqui (BSCS) 4  For Loop  - loops through a block of code a number of times  While Loop  loops through a block of code while a specified condition is true  do/while Loop  also loops through a block of code while a specified condition is true  for/in Loop  loops through the properties of an object
  • 5. For Loops Presented by Muhammad Ehtisham Siddiqui (BSCS) 5  The 'for' loop is the most compact form of looping. It includes the following three important parts  The loop initialization where we initialize our counter to a starting value. The initialization statement is executed before the loop begins.  The test statement which will test if a given condition is true or not. If the condition is true, then the code given inside the loop will be executed, otherwise the control will come out of the loop.  The iteration statement where you can increase or decrease your counter.  Syntax:- for (initialization; test condition; iteration statement) { Statement(s) to be executed if test condition is true }
  • 6. Sample Code Presented by Muhammad Ehtisham Siddiqui (BSCS) 6 <html> <body> <script type="text/javascript"> var count; document.write("Starting Loop" + "<br />"); for(count = 0; count < 10; count++){ document.write("Current Count : " + count ); document.write("<br />"); } document.write("Loop stopped!"); </script> <p>Set the variable to different value and then try...</p> </body> </html> OUTPUT: Starting Loop Current Count : 0 Current Count : 1 Current Count : 2 Current Count : 3 Current Count : 4 Current Count : 5 Current Count : 6 Current Count : 7 Current Count : 8 Current Count : 9 Loop stopped! Set the variable to different value and then try...
  • 7. Sample Code(Try It) Presented by Muhammad Ehtisham Siddiqui (BSCS) 7 <html> <body> <script type="text/javascript"> var count; var table=2; document.write("Starting Loop" + "<br />"); for(count = 1; count <= 10; count++){ document.write(table+" X "+ count+" = "+table*count ); document.write("<br />"); } document.write("Loop stopped!"); </script> </body> </html> Output: Starting Loop 2 X 1 = 2 2 X 2 = 4 2 X 3 = 6 2 X 4 = 8 2 X 5 = 10 2 X 6 = 12 2 X 7 = 14 2 X 8 = 16 2 X 9 = 18 2 X 10 = 20 Loop stopped!
  • 8. While loop Presented by Muhammad Ehtisham Siddiqui (BSCS) 8  The while statement creates a loop that is executed while a specified condition is true.  The loop will continue to run as long as the condition is true. It will only stop when the condition becomes false.  Syntax: while (Condition) { Statement(s) to be executed if expression is true }
  • 9. Example Code (Try It) Presented by Muhammad Ehtisham Siddiqui (BSCS) 9 <html> <head> <script type="text/javascript"> <!-- var count = 0; document.write("Starting Loop "); while (count < 10){ document.write("Current Count : " + count + "<br />"); count++; } document.write("Loop stopped!"); //--> </script></head> <body> <p>Set the variable to different value and then try...</p> </body> </html> OUTPUT: Starting Loop Current Count : 0 Current Count : 1 Current Count : 2 Current Count : 3 Current Count : 4 Current Count : 5 Current Count : 6 Current Count : 7 Current Count : 8 Current Count : 9 Loop stopped! Set the variable to different value and then try...
  • 10. Example Code (Try It) Presented by Muhammad Ehtisham Siddiqui (BSCS) 10 <html> <head> <script type="text/javascript"> <!-- var count = 1; document.write("Starting Loop <br/> "); while (count < 10){ document.write(5+ " X " + count + " = "+count*5+" <br />"); count++; } document.write("Loop stopped!"); //--> </script></head> <body> <p>Set the variable to different value and then try...</p> </body> </html> Output: Starting Loop 2 X 1 = 2 2 X 2 = 4 2 X 3 = 6 2 X 4 = 8 2 X 5 = 10 2 X 6 = 12 2 X 7 = 14 2 X 8 = 16 2 X 9 = 18 2 X 10 = 20 Loop stopped!
  • 11. Do While Loop Presented by Muhammad Ehtisham Siddiqui (BSCS) 11  The do...while loop is similar to the while loop except that the condition check happens at the end of the  This means that the loop will always be executed at least once, even if the condition is false.  Syntax: do{ Statement(s) to be executed; } while (expression);
  • 12. Code Presented by Muhammad Ehtisham Siddiqui (BSCS) 12 <html> <body> <script type="text/javascript"> <!-- var count = 0; document.write("Starting Loop" + "<br />"); do{ document.write("Current Count : " + count + "<br />"); count++; } while (count < 5); document.write ("Loop stopped!"); //--> </script> <p>Set the variable to different value and then try...</p> </body> </html> OUTPUT Starting Loop Current Count : 0 Current Count : 1 Current Count : 2 Current Count : 3 Current Count : 4 Loop stopped!Set the variable to different value and then try...
  • 13. Code Presented by Muhammad Ehtisham Siddiqui (BSCS) 13 <html> <body> <script type="text/javascript"> <!-- var count = 1; document.write("Starting Loop" + "<br />"); do{ document.write("6 * " + count +" = "+count*6+ "<br />"); count++; } while (count <=10); document.write ("Loop stopped!"); //--> </script> <p>Set the variable to different value and then try...</p> </body> </html> Starting Loop 6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36 6 * 7 = 42 6 * 8 = 48 6 * 9 = 54 6 * 10 = 60 Loop stopped! Set the variable to different value and then try...
  • 14. For in Loop Presented by Muhammad Ehtisham Siddiqui (BSCS) 14  The for...in loop is used to loop through an object's properties.  In each iteration, one property from object is assigned to variablename and this loop continues till all the properties of the object are exhausted.  Syntax: for (variablename in object){ statement or block to execute }
  • 15. Code Presented by Muhammad Ehtisham Siddiqui (BSCS) 15 <!DOCTYPE html> <html> <body> <p>Click the button to loop through the properties of an object.</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { var person = {fname:"John", lname:"Doe", age:25}; var text = ""; var x; for (x in person) { text += person[x] + " "; } document.getElementById("demo").innerHTML = text; } </script> </body> </html>
  • 16. Array Presented by Muhammad Ehtisham Siddiqui (BSCS) 16  JavaScript arrays are used to store multiple values in a single variable.  An array is a special variable, which can hold more than one value at a time.  If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this: var car1 = "Saab"; var car2 = "Volvo"; var car3 = "BMW";  owever, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300?  The solution is an array! var cars = ["Saab", "Volvo", "BMW"];
  • 17. Creating an Array Presented by Muhammad Ehtisham Siddiqui (BSCS) 17  Using an array literal is the easiest way to create a JavaScript Array.  Syntax: var array_name = [item1, item2, ...];  Spaces and line breaks are not important. A declaration can span multiple lines: var cars = [ "Saab", "Volvo", "BMW" ];  The following example also creates an Array, and assigns values to it: var cars = new Array("Saab", "Volvo", "BMW");
  • 18. Example Presented by Muhammad Ehtisham Siddiqui (BSCS) 18 <!DOCTYPE html> <html> <body> <h2>JavaScript Arrays</h2> <p id="demo"></p> <script> var person = []; person[0] = "John"; person[1] = "Doe"; person[2] = 46; document.getElementById("demo").innerHTML = person[0] + " " + person.length; </script> </body> </html>
  • 19. Example Presented by Muhammad Ehtisham Siddiqui (BSCS) 19 <!DOCTYPE html> <html> <body> <h2>JavaScript Arrays</h2> <p>Avoid using new Array(). Use [] instead.</p> <p id="demo"></p> <script> //var points = new Array(40, 100, 1, 5, 25, 10); var points = [40, 100, 1, 5, 25, 10]; document.getElementById("demo").innerHTML = points[0]; </script> </body> </html>
  • 20. Example(sort) Presented by Muhammad Ehtisham Siddiqui (BSCS) 20 <!DOCTYPE html> <html><body> <h2>JavaScript Array Sort</h2> <p>The sort() method sorts an array alphabetically.</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo").innerHTML = fruits; function myFunction() { fruits.sort(); document.getElementById("demo").innerHTML = fruits; } </script> </body> </html>
  • 21. Example(reverse) Presented by Muhammad Ehtisham Siddiqui (BSCS) 21 <!DOCTYPE html><html><body> <h2>JavaScript Array Sort</h2> <p>The sort() method sorts an array alphabetically.</p> <p>The reverse() method reverts the elements.</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo").innerHTML = fruits; function myFunction() { fruits.sort(); fruits.reverse(); document.getElementById("demo").innerHTML = fruits; } </script></body></html>
  • 22. 2 Dimensional Array Presented by Muhammad Ehtisham Siddiqui (BSCS) 22  var arr = [[],[]]; // 2 dimensional array  To enter data in two dimentional array you can use  arr[0][2] = 'Hi Mr.A';  arr[1][3] = 'Hi Mr.B';  To Read tw dimenstional array  alert(arr[0][2]);  alert(arr[1][3]);
  • 23. Questions? Presented by Muhammad Ehtisham Siddiqui (BSCS) 23

Editor's Notes

  • #2: Presentation slide for courses, classes, lectures et al.
  • #3: Beginning course details and/or books/materials needed for a class/project.
  • #4: Beginning course details and/or books/materials needed for a class/project.
  • #5: Beginning course details and/or books/materials needed for a class/project.
  • #6: Beginning course details and/or books/materials needed for a class/project.
  • #7: Beginning course details and/or books/materials needed for a class/project.
  • #8: Beginning course details and/or books/materials needed for a class/project.
  • #9: Beginning course details and/or books/materials needed for a class/project.
  • #10: Beginning course details and/or books/materials needed for a class/project.
  • #11: Beginning course details and/or books/materials needed for a class/project.
  • #12: Beginning course details and/or books/materials needed for a class/project.
  • #13: Beginning course details and/or books/materials needed for a class/project.
  • #14: Beginning course details and/or books/materials needed for a class/project.
  • #15: Beginning course details and/or books/materials needed for a class/project.
  • #16: Beginning course details and/or books/materials needed for a class/project.
  • #17: Beginning course details and/or books/materials needed for a class/project.
  • #18: Beginning course details and/or books/materials needed for a class/project.
  • #19: Beginning course details and/or books/materials needed for a class/project.
  • #20: Beginning course details and/or books/materials needed for a class/project.
  • #21: Beginning course details and/or books/materials needed for a class/project.
  • #22: Beginning course details and/or books/materials needed for a class/project.
  • #24: Example graph/chart.