## 🔹 Best Practices for Beginners
1. Use `const` by default,
2. Always declare variables before use
3. Use strict equality (`===`) instead of
4. Name variables meaningfully (e.g.,
5. Comment your code for complex logic
---
### 📌 What's Next?
In Part 2, we'll cover:
➡️ Conditionals (if/else, switch)
➡️ Loops (for, while)
➡️ Functions
#LearnJavaScript #CodingBasics #WebDevelopment 🚀
Practice Exercise:
1. Create variables for your name, age, and country
2. Calculate the area of a circle (PI * r²)
3. Try different type conversions
1. Use `const` by default,
let when needed, avoid var 2. Always declare variables before use
3. Use strict equality (`===`) instead of
== 4. Name variables meaningfully (e.g.,
userAge not x) 5. Comment your code for complex logic
---
### 📌 What's Next?
In Part 2, we'll cover:
➡️ Conditionals (if/else, switch)
➡️ Loops (for, while)
➡️ Functions
#LearnJavaScript #CodingBasics #WebDevelopment 🚀
Practice Exercise:
1. Create variables for your name, age, and country
2. Calculate the area of a circle (PI * r²)
3. Try different type conversions
❤3
# 📚 JavaScript Tutorial - Part 2/10: Control Flow & Functions
#JavaScript #WebDev #Programming #Beginners
Welcome to Part 2 of our JavaScript series! Today we'll master decision-making and functions - the building blocks of programming logic.
---
## 🔹 Conditional Statements
Control program flow based on conditions.
### 1. if-else Statement
### 2. Ternary Operator (Shorthand if-else)
### 3. switch-case Statement
---
## 🔹 Loops
Execute code repeatedly.
### 1. for Loop
### 2. while Loop
### 3. do-while Loop
### 4. for...of Loop (Arrays)
### 5. for...in Loop (Objects)
---
## 🔹 Functions
Reusable blocks of code.
### 1. Function Declaration
### 2. Function Expression
### 3. Arrow Functions (ES6+)
### 4. Default Parameters
### 5. Rest Parameters
---
## 🔹 Practical Example: Grade Calculator
---
## 🔹 Scope in JavaScript
### 1. Global Scope
### 2. Function Scope (var)
### 3. Block Scope (let/const)
---
## 🔹 Error Handling
### 1. try-catch-finally
### 2. Throwing Custom Errors
---
## 🔹 Best Practices
1. Use strict equality (
2. Prefer const/let over var
3. Keep functions small/single-purpose
4. Always handle errors
5. Use descriptive names for functions/variables
---
### 📌 What's Next?
In Part 3, we'll cover:
➡️ Arrays & Array Methods
➡️ Objects & JSON
➡️ Destructuring
#LearnJavaScript #CodingBasics #WebDevelopment 🚀
Practice Exercise:
1. Create a function to check if a number is even/odd
2. Write a loop that prints prime numbers up to 20
3. Make a temperature converter function (Celsius ↔️ Fahrenheit)
#JavaScript #WebDev #Programming #Beginners
Welcome to Part 2 of our JavaScript series! Today we'll master decision-making and functions - the building blocks of programming logic.
---
## 🔹 Conditional Statements
Control program flow based on conditions.
### 1. if-else Statement
let age = 18;
if (age >= 18) {
console.log("You're an adult");
} else {
console.log("You're a minor");
}
### 2. Ternary Operator (Shorthand if-else)
let message = (age >= 18) ? "Adult" : "Minor";
console.log(message);
### 3. switch-case Statement
let day = 3;
let dayName;
switch(day) {
case 1: dayName = "Monday"; break;
case 2: dayName = "Tuesday"; break;
case 3: dayName = "Wednesday"; break;
default: dayName = "Unknown";
}
console.log(dayName); // "Wednesday"
---
## 🔹 Loops
Execute code repeatedly.
### 1. for Loop
for (let i = 1; i <= 5; i++) {
console.log(`Count: ${i}`);
}
// Output: 1, 2, 3, 4, 5### 2. while Loop
let count = 1;
while (count <= 5) {
console.log(count);
count++;
}
### 3. do-while Loop
let x = 1;
do {
console.log(x);
x++;
} while (x <= 5);
### 4. for...of Loop (Arrays)
const colors = ["red", "green", "blue"];
for (const color of colors) {
console.log(color);
}
### 5. for...in Loop (Objects)
const person = {name: "Ali", age: 25};
for (const key in person) {
console.log(`${key}: ${person[key]}`);
}---
## 🔹 Functions
Reusable blocks of code.
### 1. Function Declaration
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("Ali")); // "Hello, Ali!"### 2. Function Expression
const square = function(x) {
return x * x;
};
console.log(square(5)); // 25### 3. Arrow Functions (ES6+)
const add = (a, b) => a + b;
console.log(add(2, 3)); // 5
### 4. Default Parameters
function createUser(name, role = "user") {
console.log(`${name} is a ${role}`);
}
createUser("Ali"); // "Ali is a user"### 5. Rest Parameters
function sum(...numbers) {
return numbers.reduce((total, num) => total + num, 0);
}
console.log(sum(1, 2, 3)); // 6---
## 🔹 Practical Example: Grade Calculator
function calculateGrade(score) {
if (score >= 90) return "A";
else if (score >= 80) return "B";
else if (score >= 70) return "C";
else return "F";
}
const studentScore = 85;
console.log(`Grade: ${calculateGrade(studentScore)}`); // "Grade: B"---
## 🔹 Scope in JavaScript
### 1. Global Scope
const globalVar = "I'm global";
function test() {
console.log(globalVar); // Accessible
}
### 2. Function Scope (var)
function test() {
var functionVar = "I'm function-scoped";
}
console.log(functionVar); // Error: Not accessible### 3. Block Scope (let/const)
if (true) {
let blockVar = "I'm block-scoped";
}
console.log(blockVar); // Error: Not accessible---
## 🔹 Error Handling
### 1. try-catch-finally
try {
// Risky code
nonExistentFunction();
} catch (error) {
console.log("Error occurred:", error.message);
} finally {
console.log("This always executes");
}### 2. Throwing Custom Errors
function divide(a, b) {
if (b === 0) throw new Error("Cannot divide by zero!");
return a / b;
}---
## 🔹 Best Practices
1. Use strict equality (
===) over == 2. Prefer const/let over var
3. Keep functions small/single-purpose
4. Always handle errors
5. Use descriptive names for functions/variables
---
### 📌 What's Next?
In Part 3, we'll cover:
➡️ Arrays & Array Methods
➡️ Objects & JSON
➡️ Destructuring
#LearnJavaScript #CodingBasics #WebDevelopment 🚀
Practice Exercise:
1. Create a function to check if a number is even/odd
2. Write a loop that prints prime numbers up to 20
3. Make a temperature converter function (Celsius ↔️ Fahrenheit)
❤5