0% found this document useful (0 votes)
2 views22 pages

Conditional Logic Loops and Operators 1

The document explains conditional logic in JavaScript, including the use of 'if' statements, 'else' clauses, and the ternary operator for branching based on conditions. It also covers logical operators (AND, OR, NOT, and Nullish Coalescing) and their precedence. Additionally, it introduces loops such as 'while', 'do...while', and 'for' for repeating code execution.

Uploaded by

254manbush
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views22 pages

Conditional Logic Loops and Operators 1

The document explains conditional logic in JavaScript, including the use of 'if' statements, 'else' clauses, and the ternary operator for branching based on conditions. It also covers logical operators (AND, OR, NOT, and Nullish Coalescing) and their precedence. Additionally, it introduces loops such as 'while', 'do...while', and 'for' for repeating code execution.

Uploaded by

254manbush
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

CONDITIONAL LOGIC

LOOPS AND OPERATORS


CONDITIONAL BRANCHING: IF, '?'

• Sometimes, we need to perform


different actions based on different
conditions.
• To do that, we can use
the if statement
THE “IF” STATEMENT

• It evaluates a condition in parentheses and, if the


result is true, executes a block of code.
• Example
let name = prompt(‘guess my name’,’ ’);
if ( name == "Doe"){
alert(‘perfect guess’);
} // this is a simple equality check
CONTINUED

• To execute more than one statement, we have to wrap our code


block inside curly braces:
EXAMPLE
if (name == "Dan") {
alert( "That's correct!" );
alert( "You're so smart!" );
}
THE “ELSE” CLAUSE

• The if statement may contain an optional “else” block. It executes


when the condition is falsy.
let name = prompt(‘guess my name’,’’);
if ( name == Dan){
alert(‘perfect guess’);
} else {
Alert (‘You must be joking’);
}
BOOLEAN CONVERSION

• The if (…) statement evaluates the expression in its


parentheses and converts the result to a Boolean
• FALSY VALUES will be
 0, empty string “”, null, undefined NaN

• All other values return a true hence (TRUTHY)


SEVERAL CONDITIONS: “ELSE IF”
• Used when we’d like to test several variants of a
condition
EXAMPLE
let year = prompt('In which year was the ECMAScript-2015
specification published?', ‘’);
if (year < 2015) {
alert( 'Too early...' );
} else if (year > 2015) {
alert( 'Too late' );
} else {
alert( 'Exactly!' );
}
CONDITIONAL OPERATOR
‘?’
• Also referred to as Ternary or question mark operator

SYNTAX

let result = condition ? value1 : value2;


EXAMPLE
let accessAllowed = age > 18 ? true : false;
Value1 is displayed if condition is true while
value 2 if the condition is falsy
MULTIPLE ‘?’
• A sequence of question mark operators ? can return a value that depends on more
than one condition
EXAMPLE

let age = prompt('age?', 18);


let message = (age < 3) ? ‘Very young!' : (age < 18) ?
'Hello!' : (age < 100) ? 'Greetings Legend!' : 'What an unusual
age!’;
alert( message );
LOGICAL OPERATORS

• There are four logical operators in


JavaScript: || (OR), && (AND), ! (NOT), ?? (Nullish
Coalescing)
|| (OR)
The “OR” operator is represented with two vertical line
symbols:
result = a || b;
If any of its arguments are true, it returns true, otherwise it
returns false
MORE ON OR…

• Most of the time, OR || is used in an if statement to test if any of the given conditions is true.

EXAMPLE
let hour = 12;
let isWeekend = true;
if (hour < 10 || hour > 18 || isWeekend) {
alert( 'The office is closed.' ); // it is the weekend
}
&& (AND)

• The AND operator is represented with two ampersands &&


• result = a && b;
• AND returns true if both operands are truthy
and false otherwise:
• Recap truth tables
alert( true && true ); // true
//false otherwise
MORE ON AND…

• AND stops execution immediately it encounters a falsy value


EXAMPLE

let hour = 12;


let minute = 30;
if (hour == 12 && minute == 30) {
alert( 'The time is 12:30' );
}
PRECEDENCE OF AND && IS HIGHER THAN
OR ||
• The precedence of AND && operator is higher than
OR ||
• So the code a && b || c && d
is essentially the same as (a && b) || (c && d).
! (NOT)
• The boolean NOT operator is represented with an
exclamation sign !
• The syntax is pretty simple:
result = !value;
• It simply returns the inverse of a value converted
to Boolean
• E.g. alert( !true ); // false
alert( !0 ); // true
NULLISH COALESCING OPERATOR '??'

• ?? returns the first argument if it’s not null/undefined. Otherwise,


the second one.
• The result of a ?? b is:
if a is defined, then a,
if a isn’t defined, then b.
• It’s just a nice syntax to get the first “defined” value of the two.
• The common use case for ?? is to provide a default value ->
CONTINUED…

• let user;
• alert(user ?? "Anonymous");
// Anonymous (user not defined)
EX2
let user = "John";
alert(user ?? "Anonymous"); // John (user defined)
CONTINUED…
EXAMPLE
let firstName = null;
let lastName = null;
let nickName = "Supercoder"; // shows the first defined value:
alert(firstName ?? lastName ?? nickName ?? "Anonymous"); //
Supercoder
NOTE: Using OR|| OPERATOR STILL RETURNS THE SAME OUTPUT
OR returns the first truthy value while
nullish coalescing operator ?? Returns first defined value
Avoid using ?? With && or || unless you introduce brackets
LOOPS: WHILE AND FOR
• Loops are a way to repeat the same code multiple times.

The “while” loop


Syntax
while (condition) {
// code
}
EXAMPLE let i = 0;
while (i < 3) { // shows 0, then 1, then 2
alert( i );
i++;
THE “DO…WHILE” LOOP
• used when you want the body of the loop to execute at least once
• In this loop, The condition check is moved below the loop body
• Syntax do {
// loop body
} while (condition);
EXAMPLE let i = 0;
do {
alert( `first ${i}` );
i++;
} while (i < 3);
THE “FOR” LOOP
• It is more complex, but it’s also the most commonly used loop.
• Syntax for (begin; condition; step) {
// ... loop body ...
}
Example
for (let i = 0; i < 3; i++) { // shows 0, then 1, then 2
alert(i);
}
QUESTIONS

You might also like