Control statements help your program make decisions based on conditions.
✅ 1. if Statement
Runs code only if condition is true
let age = 18;if (age >= 18) { console.log("You can vote"); }
👉 If condition is true → code runs 👉 If false → nothing happens
✅ 2. if...else
Runs one block if true, another if false
let age = 16;if (age >= 18) { console.log("Adult"); } else { console.log("Minor"); }
✅ 3. else if
Used when checking multiple conditions
let marks = 75;if (marks >= 90) { console.log("Grade A"); } else if (marks >= 70) { console.log("Grade B"); } else if (marks >= 50) { console.log("Grade C"); } else { console.log("Fail"); }
✅ 4. switch Statement
Better alternative when checking many fixed values
let day = 2;switch (day) { case 1: console.log("Monday"); break; case 2: console.log("Tuesday"); break; case 3: console.log("Wednesday"); break; default: console.log("Invalid day"); }
👉 break stops execution 👉 Without break, next cases also run
🔹 🔁 Loops (Repeat Code)
Loops help run code multiple times automatically
✅ 1. for Loop
Used when you know how many times to run
for (let i = 1; i <= 5; i++) { console.log(i); }
👉 Output: 1 2 3 4 5
✅ 2. while Loop
Runs while condition is true
let i = 1;while (i <= 5) { console.log(i); i++; }
✅ 3. do...while
Runs at least once, even if condition is false
let i = 1;do { console.log(i); i++; } while (i <= 5);
✅ 4. break
Stops loop immediately
for (let i = 1; i <= 10; i++) { if (i === 5) { break; } console.log(i); }
👉 Output: 1 2 3 4
✅ 5. continue
Skips one iteration
for (let i = 1; i <= 5; i++) { if (i === 3) { continue; } console.log(i); }
👉 Output: 1 2 4 5
🧠 Practice Programs
🎯 1. Number Guessing Game
let secret = 7; let guess;while (guess !== secret) { guess = Number(prompt("Guess a number (1-10):")); if (guess === secret) { console.log("🎉 Correct!"); } else { console.log("❌ Try again"); } }
👉 Concepts used:
while loop
if else
🎯 2. Multiplication Table Generator
let num = Number(prompt("Enter a number:"));for (let i = 1; i <= 10; i++) { console.log(num + " x " + i + " = " + (num * i)); }
Cybersecurity is the practice of protecting systems, networks, and data from digital attacks. These attacks are usually aimed at accessing, changing, or destroying sensitive information.
Confidentiality: Protects data from unauthorized access.
Integrity: Keeps information accurate and unchanged.
Availability: Ensures data and systems are accessible when needed.
To add underscores between words in a JavaScript string, effectively replacing spaces with underscores, you can use the replace() or replaceAll() methods, or a combination of split() and join().
1. Using replace() with a Regular Expression:
This method uses a regular expression to find all occurrences of spaces and replaces them with an underscore. The /g flag ensures a global replacement (all occurrences, not just the first).
JavaScript
let str = "This is a sample string"; let result = str.replace(/ /g, "_"); console.log(result); // Output: This_is_a_sample_string
2. Using replaceAll():
The replaceAll() method directly replaces all occurrences of a specified substring with another.
JavaScript
let str = "Another example string"; let result = str.replaceAll(" ", "_"); console.log(result); // Output: Another_example_string
3. Using split() and join():
This method first splits the string into an array of words using space as a delimiter, and then joins the array elements back into a string using an underscore as the separator.
JavaScript
let str = "Words separated by spaces"; let result = str.split(" ").join("_"); console.log(result); // Output: Words_separated_by_spaces
You can create user preferences in JavaScript by storing the choice when the user clicks something, and then reloading it whenever the page is opened again. The usual way is to use localStorage or cookies.
Here’s a step-by-step example:
Example: Save User Theme Preference (Dark/Light Mode)
HTML
<button id="themeToggle">Toggle Theme</button>
<p>Hello! This text will change theme.</p>