- 1. Conditional Statements in JavaScript
- [[#1. Conditional Statements in JavaScript#Definition:|Definition:]]
- [[#1. Conditional Statements in JavaScript#a.
ifStatement|a.ifStatement]] - [[#1. Conditional Statements in JavaScript#b.
if...elseStatement|b.if...elseStatement]] - [[#1. Conditional Statements in JavaScript#c.
if...else if...elseLadder|c.if...else if...elseLadder]] - [[#1. Conditional Statements in JavaScript#d. Ternary Operator (
? :)|d. Ternary Operator (? :)]] - [[#1. Conditional Statements in JavaScript#e.
switchStatement|e.switchStatement]]
- 🔁 2. Loops in JavaScript
- [[#🔁 2. Loops in JavaScript#Definition:|Definition:]]
- [[#🔁 2. Loops in JavaScript#a.
forLoop|a.forLoop]] - [[#🔁 2. Loops in JavaScript#b.
whileLoop|b.whileLoop]] - [[#🔁 2. Loops in JavaScript#c.
do...whileLoop|c.do...whileLoop]] - [[#🔁 2. Loops in JavaScript#d.
for...inLoop|d.for...inLoop]] - [[#🔁 2. Loops in JavaScript#e.
for...ofLoop (ES6)|e.for...ofLoop (ES6)]] - [[#🔁 2. Loops in JavaScript#f.
breakandcontinue|f.breakandcontinue]]
1. Conditional Statements in JavaScript
Definition:
Conditional statements are used to make decisions in code. They allow the program to execute different blocks of code based on whether a condition evaluates to true or false.
a. if Statement
Syntax:
if (condition) {
// block of code executed if condition is true
}Example:
let age = 18;
if (age >= 18) {
console.log("Eligible to vote");
}b. if...else Statement
Syntax:
if (condition) {
// true block
} else {
// false block
}Example:
let marks = 45;
if (marks >= 50) {
console.log("Pass");
} else {
console.log("Fail");
}c. if...else if...else Ladder
Syntax:
if (condition1) {
// block 1
} else if (condition2) {
// block 2
} else {
// default block
}Example:
let grade = 85;
if (grade >= 90) {
console.log("A");
} else if (grade >= 80) {
console.log("B");
} else {
console.log("C or below");
}d. Ternary Operator (? :)
A shorthand for if...else. Returns a value based on a condition.
Syntax:
condition ? valueIfTrue : valueIfFalseExample:
let access = age >= 18 ? "Allowed" : "Denied";e. switch Statement
Used for multiple condition checking. Works well with discrete values.
Syntax:
switch (expression) {
case value1:
// block
break;
case value2:
// block
break;
default:
// default block
}Example:
let day = 2;
switch (day) {
case 1: console.log("Monday"); break;
case 2: console.log("Tuesday"); break;
default: console.log("Another day");
}