Js Sem2 Lec3
Js Sem2 Lec3
Javascript Course
conditionals and
loops
-Vishal Sharma
@newtonschool
Table of Contents
• Conditional Statements
• Ternary Operator
• Assignment Operator
• Loops
02
Conditional Statements
in Javascript
3
What are conditional statements?
PRIMITIVES
Types of conditional statements
if-else-if
if if-else switch
else
if statement
I am an adult.
condition
if-else: Building on if statement
If you understand the if statement, adding the else part is straightforward. It simply
provides an alternative action when the condition is false.
if(condition) {
// body of if
if statement + else = }
else {
// body of else
}
if-else: Building on if statement
I am a minor.
Going beyond: if-else-if-else ladder
The if-else-if-else ladder helps the program check multiple conditions one by one
and take an action based on the first condition that’s true.
Going beyond: if-else-if-else ladder
The ternary operator is a concise way to write an if-else statement in a single line. It
evaluates a condition and returns one value if true, and another if false.
Ternary Operator: example
The ternary operator simplifies if-else code when only one condition is involved.
Ternary Operator: Comparison with if..else
/
Assignment Operators: Example
Let’s have a look at an example:-
let
for loop: example
Let’s see an example:-
Here console.log
statement is printed
5 times as loop runs
for exact 5 times.
while loop
A while loop in JavaScript repeatedly executes a block of code as long as the
condition remains true, making it ideal when the number of iterations depends
on dynamic conditions.
while loop: syntax
for loop has just end condition, increment/decrement is done in body and
initialization is done before the loop starts.
while loop: example
Let’s see an example:-
40
Control statement: break
When the specified condition is met, the break statement immediately terminates
the loop and transfers control to the next statement after the loop.
Control statement: break (with)
Output:
1
2
Control statement: break (without)
Output:
1
2
3
4
5
Control statement: continue
break
Arrays and Objects
49
What are arrays?
Arrays are one of the best data structures for storing multiple values in a single
variable. Loops, such as for, while, or do-while are commonly used with arrays to
process or manipulate their elements efficiently.
Using array with for loop
Let’s use arrays along with loop. Here we will iterate array elements using for loop:-
Objects: Real world dictionary
Think of an object as a real-world dictionary, where the key is the word you're
looking up (e.g., "name" or "age"), and the value is the definition or meaning of that
word (e.g., "Alice" or 20).
Key Value
name Alice
age 20
In Javascript
Objects: Practical Example
When you have data that involves key-value pairs, objects are the most appropriate
choice. Here is an example:-
We often store
multiple objects in
an array is to keep
things organized.
Object Array: Iterating using for loop
And then iterate over them using for loop:-
Object Array: Iterating using for…in loop
It would be lot simpler if we use for…in loop instead:-
63
Thanks
for
watching!
64