CONTROL FLOW
IN JAVASCRIPT
AGENDA
Variables
Operators
Conditions
Loops UNDERSTANDING DATA
TYPES IN JAVASCRIPT:
Definition :
3
1. Data types define the kind of data that can be stored and
manipulated in JavaScript.
2. JavaScript has dynamic typing, meaning variables can hold
values of different types.
JavaScript has data types:
1. Primitive
2. Complex
4
Control flow in JavaScript PRIMITIVE DATA TYPES
NUMBER STRING BOOLEAN UNDEFINED NULL
• • Represents a • Represents the
• Represents • Represents either
true or false.
• • •
•
•
5
COMPLEX DATA TYPES
OBJECT ARRAY
• Represents a collection of key-value • Represents an ordered list of values. pairs. •
Example: [1, 2, 3, 4]
• Example: { name: "John", age: 25 }
JAVASCRIPT VARIABLES
Definition :
1. Variables are used to store and manipulate data in JavaScript.
2. They provide a way to label and reference values within your
code.
Variable Declaration:
6
Variables in JavaScript are declared using the (var
, let , const ) => keywords Example :
Variable Initialization:
You can declare and assign a value to a variable in a single step,
known as initialization.
Example :
7
Variable Naming Rules:
Variable names in JavaScript must follow certain rules:
• They can contain letters, digits, underscores, or dollar signs.
• They must start with a letter, underscore, or dollar sign (not a digit).
• They are case-sensitive.
Example of valid variable names :
‘myVariable’
‘_count’
8
Example of Invalid variable names :
‘2test’
‘my-variable’
‘@name’
Variable Scope
The scope of a variable refers to its visibility and accessibility within the code.
Global Scope:
1. Variables declared outside of any function or block have global scope.
2. Global variables can be accessed from anywhere in the code.
Example:
9
Local Scope :
1. Variables declared inside a function or block have local scope.
2. Local variables are only accessible within the function or block they are
declared in.
Example:
Block Scope (let, const):
10
• Variable declared with ( let , const ) keyword have block Scope.
• Block scoped variables are limited to the block they are declared in , such as
condition or loops.
Reserved Word :
Reserved words that have special meanings and cannot be used as variables,
functions, methods, loop labels, or any object names.
These reserved words are part of the JavaScript language syntax and serve
specific purposes.
11
Example:
•
•
•
OPERATORS IN JAVASCRIPT
Operators are symbols used to perform
operations on values in JavaScript.
12
They enable
Arithmetic calculations,
Comparisons,
Logical operations, and more.
ARITHMETIC OPERATORS
13
Operator Description
Addition: + Adds two operands Ex:
A + B will give 30
Subtraction: - Subtracts the second operand from the first Ex:
A - B will give -10
Multiplication: * Multiply both operands Ex:
A * B will give 200
Division: / Divide the numerator by the denominator
Ex: B / A will give 2
Remainder Outputs the remainder of an integer division Ex:
B % A will give 0
(Modulus): %
Increment: ++ Increases an integer value by one Ex:
A++ will give 11
Decrement: -- Decreases an integer value by one Ex:
A-- will give 9
14
COMPARISON OPERATORS:
COMPARISON OPERATORS COMPARE VALUES AND RETURN A BOOLEAN RESULT.
15
Operator Description
Equal to: == Checks if the value of two operands are equal or not, if yes, then the
condition becomes true.
Ex: (A == B) is not true.
Not equal to: != Checks if the value of two operands are equal or not, if the values are
not equal, then the condition becomes true.
Ex: (A != B) is true.
Greater than: > Checks if the value of the left operand is greater than the value of the right
operand, if yes, then the condition becomes true. Ex: (A > B) is not true.
Less than: < Checks if the value of the left operand is less than the value of the right
operand, if yes, then the condition becomes true. Ex: (A < B) is true.
Greater than or Checks if the value of the left operand is greater than or equal to the
value of the right operand, if yes, then the condition becomes true.
equal to: >= Ex: (A >= B) is not true.
16
Less than or equal Checks if the value of the left operand is less than or equal to the
value of the right operand, if yes, then the condition becomes true.
to: <= Ex: (A <= B) is true.
17
LOGICAL OPERATORS:
.
PERFORM LOGICAL OPERATIONS AND RETURN BOOLEAN RESULTS .
18
Operator Description
AND: && If both the operands are non-zero, then the condition becomes true. Ex:
(A && B) is true.
OR: || If any of the two operands are non-zero, then the condition becomes true.
Ex: (A || B) is true
NOT: ! Reverses the logical state of its operand. If a condition is true, then the
Logical NOT operator will make it false. Ex: ! (A && B) is false
CONDITIONAL STATEMENTS: IF-ELSE
• Conditional statements allow the execution of different code blocks based on certain conditions.
19
• The "if-else" statement is one of the most commonly used conditional statements in JavaScript.
if...else if... Statement
• The ‘if...else if...’ statement is an advanced form of if…else that allows JavaScript to make a correct
decision out of several conditions
20
LOOPS: WHILE AND FOR
21
LOOPS: FOR
The ‘for’ loop is the most compact form of looping. It includes the following
three important parts:
1. The loop initialization where we initialize our counter to a starting
value. The initialization statement is executed before the loop begins.
2. 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.
3. The iteration statement where you can increase or decrease your
counter.
22
LOOPS: FOR 23
20
CODE PROBLEM QUESTIONS
1. Write a JavaScript program to declare a variable name and assign it a string value. Print
the value of the name variable to the console.
2. Write a JavaScript program to declare two variables num1 and num2 and assign them
numerical values. Calculate the sum of num1 and num2 and store the result in a
variable called sum. Print the value of sum to the console.
3. Write a JavaScript program to declare a constant variable PI and assign it the value of
3.14159. Declare another variable radius and assign it a numerical value. Calculate the
area of a circle using the formula area = PI * radius * radius and store the result in a
variable called circle Area. Print the value of circle Area to the console.
4. Write a JavaScript program that takes a number as input and checks if it is positive,
negative, or zero. Print an appropriate message to the console based on the input.
Write a JavaScript program using a while loop to print the numbers from
1 to 10 to the console.
Write a JavaScript program using a for loop to print the numbers from 1 to 10 to
THANK YOU