JavaScript Logic & Functions - Post 2

🧠 If / Else Conditions
Conditions help your code make decisions β€” like a traffic signal for your logic.

Example: Should we allow a user to vote?
let age = 18;

if (age >= 18) {
  console.log("You are eligible to vote!");
} else {
  console.log("Sorry, you must be 18+ to vote.");
}
Real-life analogy: If it's raining, take an umbrella. Else, go without one.
πŸ” Loops
Loops let you repeat actions without writing the same code multiple times.

Example: Say hello 5 times
// For loop
for (let i = 1; i <= 5; i++) {
  console.log("Hello #" + i);
}

// While loop
let count = 1;
while (count <= 5) {
  console.log("Hi #" + count);
  count++;
}

Use loops to repeat code:

  for (let i = 1; i <= 3; i++) {
    console.log("Hello " + i);
  }
  
  let count = 0;
  while (count < 3) {
    console.log(count);
    count++;
  }
      
Real-life analogy: Brushing your teeth every morning. Same action. Repeated daily.
πŸ”§ Functions
A function is a reusable block of code that performs a task. Think of it as a machine β€” give it input, it gives you output.
// Regular function
function greet(name) {
  console.log("Hello, " + name + "!");
}

greet("Alice");

// Arrow function
const add = (a, b) => a + b;

console.log(add(5, 3));

Example: A function to greet someone
  function greet(name) {
    return "Hi, " + name + "!";
  }
  
  const square = (n) => n * n;
      
Real-life analogy: A coffee machine: You give water + coffee β†’ it gives you a drink. Functions work the same way!
πŸ“š What's Next?
πŸš€ Dive into the magic of scope, hoisting, closures, and how JavaScript thinks. With relatable explanations and real-world code, you'll finally understand the quirks and powers behind this, arrow functions, and more. It’s written like a mentor is teaching you, not a textbook. No fluff, just clarity and fun.
Dive   →

β€œThis is an educational resource about JavaScript created with original content. No user data is collected.”