JavaScript
Conditional Statement and
Function
Comparison statements are used to evaluate the quality
or the difference between variables or values.
COMPARISON OPERATOR
OPERAND EXAMPLE RESULTS
== x == y Returns true if x is equal to y
=== x === y Returns true if x is equal to y and the data are of
the same type
!= x != y Returns true if x is not equal to y
!= = x != = y Returns true if x is not equal to y and/or the data
are not of the same type
COMPARISON OPERATOR
OPERAND EXAMPLE RESULTS
> x>y Returns true if x is greater than y
< x<y Returns true if x is less than y
>= x >= y Returns true if x is greater than or equal to y
<= x <= y Returns true if x is less than or equal to y
Logical operators are used to determine the logic between
expressions, variables, values. It is usually used by two or
more expressions to satisfy the needed conditions.
LOGICAL OPERATORS
OPERATOR Description EXAMPLE RESULTS
&& And (x == y) && (x > 3) Returns true if both
expressions are true
|| Or (x == y) | | (x > 3) Returns true if at least one
of the expressions is true
! Not ! (x == y) Returns true if x is not
equal to y
JavaScript also has its own conditional operator that automatically
assigns a value depending on the result of the condition.
syntax for conditional variablename = (condition) ?value1:value2:
statements
myFavorite = (color == “pink”) ?”Terrific”:”Change color”;
Conditional Statements are used to perform different
operations based on the set conditions.
Kinds of conditional Statements
• if statement = used to check if a specified condition is true
• if-else statement = used to check if the condition is true and
performs an action if the condition is false.
• if-else-if-.else statement = used to check if one or more blocks of
code can be executed.
• Switch statement = used if you want to select one of many blocks
of code to be executed.
switch statements
https://www.w3schools.com/jsref/jsref_statements.asp
FUNCTIONS AND
LOOPS
Function is a set of statements or blocks of codes
combined together for a particular use.
It is usually used so that you will not have to retype
your codes again when you needed them; you can
simply call it to execute it.
Functions You may call a function anywhere within the page or
even from an external Javascript file.
Functions can be defined anywhere in the head section
or body section; however it is ideal to put the function
in the head section so that you are assured that it is
loaded properly.
Syntax of a function
function
functionname
(var1,var2,var3…,varN) functionname is the name of the
function. var1,var2,…, varn are
{
parameters
//place your codes here
}
What is Loop?
• There are times when you want a block code repeated over and over again until a
certain condition is satisfied.
• JavaScript Loops
• For Loop – It loops through a block of code within a specified number of times.
• While Loop – It loops through a block of code until the condition is satisfied.
• Do While Loop - It is like while loop; however, it will execute the set of codes at
least once.
• For in loop- It loops through the elements of an array.
Different Kinds of Loops
JavaScript supports different kinds of loops:
• for - loops through a block of code a number of times
• for/in - loops through the properties of an object
• for/of - loops through the values of an iterable object
• while - loops through a block of code while a specified condition is true
• do/while - also loops through a block of code while a specified condition is true
For Loop
While Loop
The Do While Loop
The do while loop is a variant of the while
loop. This loop 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.
Syntax
do {
// code block to be executed
}
while (condition);
Example
The example below uses a do while loop. The loop
will always be executed at least once, even if the
condition is false, because the code block is
executed before the condition is tested:
The For In Loop
The JavaScript for in statement loops through
the properties of an Object:
Syntax
for (key in object) {
// code block to be executed
}
The Break Statement
You have already seen the break statement used in
an earlier chapter of this tutorial. It was used to
"jump out" of a switch() statement.
The break statement can also be used to jump out of
a loop:
The break statement "jumps out" of a loop.
The continue statement "jumps over" one
iteration in the loop.
The Continue Statement
The continue statement breaks one iteration (in the
loop), if a specified condition occurs, and continues with
the next iteration in the loop.
This example skips the value of 3: