Detailed explanations & code examples are in the JavaScript (.js) file
Click the { } button at the bottom right to view it
Master If Statements, Switch, Ternary, and String Methods
If statements run code only when a condition is true — the foundation of all decision-making in JavaScript
== equal to | === strictly equal (checks type too) | != not equal> greater than | < less than | >= greater or equal | <= less or equal
if (condition) { // runs if condition is true } else if (anotherCondition) { // runs if first was false, this is true } else { // runs if ALL above conditions were false }
== checks value only — "5" == 5 is true (ignores type)=== checks value AND type — "5" === 5 is false=== in real code to avoid unexpected bugs!
Result will appear here...
age >= 100 → Too old messageage == 0 → Just born messageage >= 18 → Can enterage < 0 → Invalid ageelse → Must be 18+if, else if, and elseThe .checked property reads whether a checkbox or radio button is currently selected — returns true or false
name attribute, only one in the group can be selected at a timeelement.checked to read their state
// Checkbox if (myCheckBox.checked) { ... } // Radio buttons — check each one individually if (visaBtn.checked) { ... } else if (masterCardBtn.checked) { ... }
Select Payment Method:
Subscription status will appear here...
Payment method will appear here...
.checked on a checkboxelse if chainA compact shortcut for simple if/else statements — assigns a value based on a condition in one line
condition ? valueIfTrue : valueIfFalse? means "if true" and the : means "otherwise"
// Regular if/else: let greeting; if (time < 12) { greeting = "Good morning!"; } else { greeting = "Good afternoon!"; } // Same thing with ternary (one line): let greeting = time < 12 ? "Good morning!" : "Good afternoon!";
| Style | Code | Best For |
|---|---|---|
| if/else | if(x){...} else{...} |
Complex logic, multiple lines |
| Ternary | x ? a : b |
Simple one-value decisions |
if/else instead.
time < 12 ? "Good morning!" : "Good afternoon!"isStudentpurchaseAmount >= 100 ? 10 : 0Switch compares one value against many possible cases — cleaner than a long chain of else if statements
switch (value) { case 1: // runs if value === 1 break; // stops here, exits switch case 2: // runs if value === 2 break; default: // runs if NO cases matched (like else) }
break!break, JavaScript "falls through" and runs every case below the match. This is almost always a bug. Add break at the end of every case.
| Keyword | Purpose |
|---|---|
switch(value) |
The value being compared against all cases |
case x: |
Runs if value === x |
break |
Exits the switch — prevents fall-through to next case |
default: |
Runs if no case matched — like else |
switch(true) trick:switch(value), use switch(true) to write conditions in each case — useful for range checks like letter grades.
switch(true) { case score >= 90: letterGrade = "A"; break; case score >= 80: letterGrade = "B"; break; default: letterGrade = "F"; }
switch(day) with 7 cases + defaultswitch(true) with range conditionsBuilt-in functions that let you read, transform, and search through strings
string.methodName()
| Method | What It Does | Example | Result |
|---|---|---|---|
.length |
Number of characters | "Umer".length |
4 |
.charAt(i) |
Character at index | "Umer".charAt(0) |
"U" |
.indexOf(x) |
First position of x | "Umer".indexOf("m") |
1 |
.lastIndexOf(x) |
Last position of x | "Umer Azmi".lastIndexOf("m") |
7 |
.includes(x) |
Contains x? true/false | "Umer".includes("me") |
true |
.startsWith(x) |
Starts with x? true/false | "Umer".startsWith("U") |
true |
.endsWith(x) |
Ends with x? true/false | "Umer".endsWith("r") |
true |
| Method | What It Does | Example | Result |
|---|---|---|---|
.toUpperCase() |
All caps | "umer".toUpperCase() |
"UMER" |
.toLowerCase() |
All lowercase | "UMER".toLowerCase() |
"umer" |
.trim() |
Remove start/end spaces | " hi ".trim() |
"hi" |
.replaceAll(a, b) |
Replace all occurrences | "1-2-3".replaceAll("-", "") |
"123" |
.repeat(n) |
Repeat string n times | "ha".repeat(3) |
"hahaha" |
.padStart(n, x) |
Pad start until length n | "5".padStart(3, "0") |
"005" |
.padEnd(n, x) |
Pad end until length n | "5".padEnd(3, "0") |
"500" |
"Umer Azmi""123-456-7890"Extract a portion of a string by specifying a start and end index
string.slice(start, end)start — index to begin (inclusive)end — index to stop (exclusive — this character is NOT included)end, it slices to the end of the string
const name = "Umer Azmi"; // 0123 4567 ← index positions name.slice(0, 4); // "Umer" (index 0 to 3) name.slice(5); // "Azmi" (index 5 to end) name.slice(5, 9); // "Azmi" (index 5 to 8)
.indexOf() with .slice():email.slice(0, email.indexOf("@")) → gets the username from any emailemail.slice(email.indexOf("@") + 1) → gets the domain from any email
| Expression | On "UmerAzmi@gmail.com" |
Result |
|---|---|---|
.indexOf("@") |
Position of @ | 8 |
.slice(0, 8) |
Everything before @ | "UmerAzmi" |
.slice(9) |
Everything after @ | "gmail.com" |
"Umer Azmi" using fixed indices"UmerAzmi@gmail.com" using .indexOf()Calling multiple methods one after another in a single line — each method's output feeds into the next
string.trim().toUpperCase().slice(0, 5)trim() removes spaces → Step 2: toUpperCase() capitalizes → Step 3: slice(0,5) cuts first 5 chars
// Without chaining — 4 lines let input = userInput.trim(); let first = input.charAt(0).toUpperCase(); let rest = input.slice(1).toLowerCase(); let result = first + rest; // With chaining — 1 line, same result let result = userInput.trim().charAt(0).toUpperCase() + userInput.trim().slice(1).toLowerCase();
Formatted result will appear here...
Capitalizes the first letter and lowercases the rest — e.g. "jOHN dOE" → "John doe"
Loading...