WebPHP Unit22 - PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Web Programming using PHP

Unit II JavaScript – Decision Control Statements


JS
JavaScript we have the following conditional statements
if to specify a block of code to be executed, if a specified condition is true
if (condition) {
// block of code to be executed if the condition is true
}

else to specify a block of code to be executed, if the same condition is false


if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}

else-if to specify a new condition to test, if the first condition is false


if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
NSS College Rajakumari, Dept. of Computer Applications 1
Web Programming using PHP
Unit II JavaScript – Decision Control Statements
JS is used to perform different actions based on different
Switch conditions.
switch(expression) { The switch expression is
case x:
// code block
evaluated once and the value of
break; the expression is compared with
case y: the values of each case. If there
// code block is a match, the associated block
break;
default: of code is executed. If there is
// code block no match, the default code
} block is executed.
JavaScript – Loops
Loops Description
for loops through a block of code a number of times Differences and
for/in loops through the properties of an object Flowcharts
for/of loops through the values of an iterable object
while loops through a block of code while a specified condition is true – Entry controlled
do/while loops through a block of code while a specified condition is true – Exit controlled
NSS College Rajakumari, Dept. of Computer Applications 2
Web Programming using PHP
Unit II For Loop
JS
for (statement 1; statement 2; statement 3) {
// code block to be executed
}

Statement 1 is executed (one time) before


the execution of the code block. Statement 2
defines the condition for executing the code
block. Statement 3 is executed (every time)
after the code block has been executed.

For/in Loop

NSS College Rajakumari, Dept. of Computer Applications 3


Web Programming using PHP
Unit II For/of Loop
JS
loops through the values of an iterable such as Arrays, Strings, Maps,
NodeLists, and more.

for (variable of iterable) { For every iteration the


// code block to be executed value of the next property
} is assigned to the variable.

WhileLoop
loops through a block of code as long as a specified condition is true.

while (condition) {
// code block to be executed
}

NSS College Rajakumari, Dept. of Computer Applications 4


Web Programming using PHP
Unit II Do/While Loop
JS
will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the
condition is true.

do {
// code block to be executed
}
while (condition);

NSS College Rajakumari, Dept. of Computer Applications 5


Web Programming using PHP
Unit II JavaScript Functions
JS A JavaScript function is defined with the function
is a block of code designed to perform a particular task. keyword, followed by a name, followed by parentheses
(). Function names can contain letters, digits,
function name(parameter1, parameter2, parameter3) underscores, and dollar signs (same rules as
{ variables).The parentheses may include parameter
// code to be executed names separated by commas
} (parameter1, parameter2, ...). The code to be executed,
by the function, is placed inside curly brackets {}.

Function parameters The execution of function


(arguments) are the values definition will be stopped
received by the function when it when it reaches return
is invoked. Inside the function, statement. The result will be
the arguments behave as local returned to the caller
variables. statement and normal flow
of execution continues.
Function Invoking
• When an event occurs (when a user clicks a button)
• When it is invoked (called) from JavaScript code
• Automatically (self invoked)
NSS College Rajakumari, Dept. of Computer Applications 6
Web Programming using PHP
Unit II JavaScript Functions
JS

JavaScript Object Constructor


An Object Constructor is merely a regular JavaScript function. The difference is that, a constructor function is
called via the new operator.
Advantage is, an object "type" can be created, that can be used multiple times without having to redefine the
object every time to meet each particular instance's needs. The standard way to achieve this is to use the Object
Constructor function.
NSS College Rajakumari, Dept. of Computer Applications 7
Web Programming using PHP
Unit II JavaScript Object Constructor - example
JS
Object constructor function

Creating same “type” Adding new property


objects
Adding new method

this is a keyword, not a variable. In a


constructor function this does not have
a value. It is a substitute for the new
object. The value of this will become the
new object when a new object is
created.
Click here for JavaScript Prototype
NSS College Rajakumari, Dept. of Computer Applications 8
Web Programming using PHP

References
1. https://www.tutorialspoint.com
2. https://www.geeksforgeeks.org
3. https://www.w3schools.com
4. https://www.javapoint.com
5. https://developer.mozilla.org
6. https://techterms.com
7. https://www.tutorialsteacher.com
8. https://www.c-sharpcorner.com
9. http://www.javascriptkit.com/javatutors/oopjs2.shtml
10.Background vector created by grmarc - www.freepik.com
https://www.freepik.com/free-photos-
vectors/background

NSS College Rajakumari, Dept. of Computer Applications 9

You might also like