You are on page 1of 5

Web Design and Development Lesson 7

Lesson 7: Javascript Control Flow

Lesson 7: Javascript Control Flow .................................................................................................. 1


7.1 JavaScript Data Types ...................................................................................................... 2
7.2 JavaScript Operators ....................................................................................................... 2
7.3 JavaScript Decision Making Statements ......................................................................... 2
7.3.1. If statement............................................................................................................... 2
7.3.2. If..else statement ...................................................................................................... 2
7.3.3. If..else if statement ................................................................................................... 3
7.3.4. Switch statement ...................................................................................................... 3
7.4 JavaScript Loops............................................................................................................... 4
7.4.1. For loop ..................................................................................................................... 4
7.4.2. While loop ................................................................................................................. 4
7.4.3. Do..While loop .......................................................................................................... 5
Lesson 7 Revision Question ........................................................................................................ 5

Compiled by: Karari E.K email: cats.mwalimu@gmail.com 1


Web Design and Development Lesson 7

7.1 JavaScript Data Types


JavaScript Data types are used to identify the type of data that is stored inside a
variable during the script execution.
So JavaScript data types are basically for identification purposes to know what is
being stored in the variable, when it's stored, not before that.
1. String – E.g var str= “Web Program”;
2. Boolean- E.g var isOn= true;
3. Number – var age=25;

7.2 JavaScript Operators


Operators are used to performing operations on the operands. Operators are useful to modify
or change data by manipulating the respective variables. For example, there are two variables a
and b and we want to add the values of both of these variables, to do so we can use arithmetic
operator + to perform addition of these two variables. Types of operators:
1. Arithmetic eg +, -, *, / ,%, ++,--
2. Comparison eg ==, !=, >, <, >=
3. Logical eg && , || , !
4. Assignment i.e =
7.3 JavaScript Decision Making Statements
7.3.1. If statement
It evaluates the content only if expression is true.

7.3.2. If..else statement


It evaluates the content whether condition is true or false.

Compiled by: Karari E.K email: cats.mwalimu@gmail.com 2


Web Design and Development Lesson 7

7.3.3. If..else if statement


It evaluates the content only if expression is true from several expressions.

7.3.4. Switch statement


The switch statement allows to make a decision from the number of choices.
If a match is found to a case label, the program executes the associated statement.
If no match is found with any of the case statements, only the statements following the
default are executed.

Compiled by: Karari E.K email: cats.mwalimu@gmail.com 3


Web Design and Development Lesson 7

7.4 JavaScript Loops


They are used to iterate the piece of code using for, while, or do..while loops. It makes the code
compact. It is mostly used in array.
7.4.1. For loop

7.4.2. While loop

Compiled by: Karari E.K email: cats.mwalimu@gmail.com 4


Web Design and Development Lesson 7

7.4.3. Do..While loop

Lesson 7 Revision Question

Compiled by: Karari E.K email: cats.mwalimu@gmail.com 5

You might also like