Lecture 12
Lecture 12
2023/6/15
Contents
• Declaring Variables
• JavaScript Datatypes
• JavaScript Variable Scope
• JavaScript Variable Names
• JavaScript Reserved Words
• JavaScript Operators
• JavaScript Conditional Statements
• JavaScript Iteration Statements/LOOPs
• JavaScript Functions
2
JavaScript Variables
3
JavaScript Datatypes
4
JavaScript Datatypes
5
JavaScript Variables
6
JavaScript Variables
7
JavaScript Variable Scope
8
JavaScript Variable Scope
9
JavaScript Variable Scope Overriding
10
JavaScript Variable Scope Overriding
11
JavaScript Variable Scope
12
JavaScript Reserved Words
13
Javascript - Operators
14
Arithmetic Operators
• + (Addition)
• - (Subtraction)
• * (Multiplication)
• / (Division)
• % (Modulus)
• ++ (Increment)
• -- (Decrement)
Note − Addition operator (+) works for Numeric as well as
Strings. e.g. "a" + 10 will give "a10".
15
Arithmetic Operators
16
Comparison Operators
• = = (Equal)
• != (Not Equal)
• > (Greater than)
• < (Less than)
• >= (Greater than or Equal to)
• <= (Less than or Equal to)
17
Comparison Operators
18
Logical Operators
19
Logical Operators
20
Bitwise Operators
• & (Bitwise AND)
• | (Bitwise OR)
• ~ (Bitwise Not)
• ^ (Bitwise XOR)
• << (Left Shift)
• >> (Right Shift)
• >>> (Right shift with Zero)
21
Bitwise Operators
22
Assignment Operators
• = (Simple Assignment )
• += (Add and Assignment)
• −= (Subtract and Assignment)
• *= (Multiply and Assignment)
• /= (Divide and Assignment)
• %= (Modules and Assignment)
23
Assignment Operators
24
JavaScript Operators
25
Conditional Operator (? :)
26
Conditional Operator (? :)
27
typeof Operator
28
typeof Operator
Here is a list of the return values for the typeof Operator.
Type String Returned by typeof
• Number "number"
• String "string"
• Boolean "boolean"
• Object "object"
• Function "function"
• Undefined "undefined"
• Null "object"
29
typeof Operator
30
JavaScript - If...Else Statement
31
JavaScript - If...Else Statement
32
JavaScript - If...Else Statement
• if statement
• if...else statement
• if...else if... statement.
33
If Statement
34
If...Else Statement:
36
If...Else If... Statement
38
Javascript - Switch Case
39
Flow Chart For Switch
40
JavaScript Switch syntax:
switch (expression)
{
case condition 1: statement(s)
break;
case condition 2: statement(s)
break; ...
case condition n: statement(s)
break;
default: statement(s)
}
41
JavaScript Switch Code Example
42
Javascript - Loops
43
The while Loop
The most basic loop in JavaScript is the while loop which would
be discussed here.
The purpose of a while loop is to execute a statement or code block
repeatedly as long as an expression is true.
Once the expression becomes false, the loop terminates.
44
The while Loop Flow Chart
45
The while Loop Syntax & Example
The syntax of while loop in JavaScript is as follows −
while (expression){
Statement(s) to be executed if expression is true
}
Example:
46
The do...while Loop
The do...while loop is similar to the while loop except that the
condition check happens at the end of the loop.
This means that the loop will always be executed at least once,
even if the condition is false.
47
The do...while Loop Flow Chart
48
The do...while Loop Syntax & Example
The syntax for do-while loop in JavaScript is as follows −
do{
Statement(s) to be executed;
} while (expression);
Note − Don’t miss the semicolon used at the end of the do...while
loop.
Example:
49
Javascript - For Loop
50
Javascript - For Loop Flow Chart
51
Javascript - For Loop Syntax
The syntax of for loop is JavaScript is as follows:
for (initialization; test condition; iteration statement){
Statement(s) to be executed if test condition is true
}
Example:
52
Javascript For...In Loop
53
Javascript For...In Loop Syntax
54
Javascript For...In Loop Example
55
Loop Control
56
Loop Control
57
The Break Statement
58
The Break Statement Flow Chart
59
The Break Statement Example
60
The continue Statement
61
The continue Statement Example
62
JavaScript Function
63
Function Definition Ways
• Function Statement
• Function Constructor
• Function Literal
64
1. Function Definition
65
Function DefinitionSyntax & Example
<script type="text/javascript">
function functionName(parameter-list)
{
Example
statements
}
</script>
66
Calling a Function
To invoke a function somewhere later in the script, you would
simply need to write the name of that function as shown in the
following code.
67
Function Parameters
68
Function Parameters Example
69
The Return Statement
70
The Return Statement Example
71
2. Function () Constructor
72
2. Function () Constructor
74
3. Function Literals
75
3. Function Literals Example
76
THANK YOU
[email protected]