Back to Cheat Sheets

Writing

JavaScript for Beginners - Part 3: Operators, Control Flow & Error Handling

Learn how to make your code "think"! In this part, we cover mathematical operators, logical comparisons, and how to use If/Else statements to control your program. We also look at try...catch to handle errors gracefully. Includes explanations in Darija.

javascript web-development programming-basics logic operators error-handling darija

January 25, 2026 · 4 min read

JavaScript for Beginners - Part 3: Operators, Control Flow & Error Handling

JavaScript for Beginners: Part 3

⚖️ Operators, Control Flow & Error Handling

Now that we know about data types, let’s learn the "grammar" to work with them. Operators are special symbols used to perform operations on our variables and values, and Control Flow allows us to decide which parts of our code should run.

1️⃣ Arithmetic Operators (العمليات الحسابية)

Used for basic mathematical calculations.

  • + Addition: Adds numbers together.
  • - Subtraction: Subtracts numbers.
  • * Multiplication: Multiplies numbers.
  • / Division: Divides numbers.
  • % Modulus: Gives the remainder of a division (e.g., 10 % 3 is 1).
  • ** Exponentiation: Raises to a power (e.g., 2 ** 3 is 8).
🇲🇦 Darija:
هادو هما العمليات الحسابية العادية اللي قرينا فالمدرسة (الجمع، الطرح، الضرب، والقسمة). كنزيدو عليهم الـ Modulus اللي كيعطيك الباقي ديال القسمة، والـ Exponentiation ديال الأُس.

2️⃣ Comparison Operators (عمليات المقارنة)

Used to compare two values. The result is always a Boolean (true or false).

  • === Strict Equality: Returns true only if both the value AND the type are the same. (Always use this!)
  • !== Strict Inequality: Returns true if values or types are different.
  • > / < : Greater than / Less than.
  • >= / <= : Greater than or equal / Less than or equal.
🇲🇦 Darija:
هادو كنخدمو بيهم باش نقارنو بين جوج قيم. ديما استعمل تلاتة ديال التساوي (===) باش تتأكد باللي القيمة ونوع البيانات بحال بحال، هكا كتفادى المشاكل فـ الكود ديالك.

3️⃣ Logical Operators (العمليات المنطقية)

Used to combine multiple boolean conditions.

  • && AND: Returns true only if both conditions are true.
  • || OR: Returns true if at least one condition is true.
  • ! NOT: Inverts the value (!true becomes false).
🇲🇦 Darija:
هادو كينفعو باش نجمعو بزاف ديال الشروط. مثلاً: "إيلا كان المستخدم مسجل (&&) وعندو الصلاحية". الـ AND كطلبهم بجوج، والـ OR كيكفيها غير واحد يكون صحيح.

4️⃣ Control Flow: If / Else (الشروط)

The most common way to control which code runs based on conditions.

  • if: Runs code only if the condition is true.
  • else if: Checks a new condition if the first one was false.
  • else: Runs if none of the previous conditions were true.
🇲🇦 Darija:
هنا فين كيبدا البرنامج "يفكر". كتقول ليه: "إيلا (if) كان هاد الشرط صحيح دير هادي، وإيلا مكانش (else) دير حاجة أخرى".

5️⃣ Error Handling: Try...Catch (معالجة الأخطاء)

This acts as a safety net to prevent your program from crashing if something goes wrong.

  • try: Place the code you suspect might cause an error here.
  • catch: This code runs only if an error occurs in the try block.
  • finally: Runs no matter what (even if there was no error).
🇲🇦 Darija:
هاد السطر ديال الكود هو "الشبكة ديال الأمان". كتقول لـ جافاسكريبت: "جربي (try) هاد الكود، وإيلا وقع فيه شي مشكل، بلا ما تسكتي الموقع كامل، شدي هاد الغلط (catch) وقولي ليا شنو وقع".

💻 Code Example

let userAge = 20;
let hasTicket = true;

// Using Comparison & Logic
if (userAge >= 18 && hasTicket) {
    console.log("Welcome to the event! 🎉");
} else {
    console.log("Access Denied.");
}

// Using Try...Catch for safety
try {
    // Imagine a function that might fail
    let result = 10 / someUndefinedVariable; 
} catch (error) {
    console.log("An error occurred: " + error.message);
} finally {
    console.log("Cleaning up resources...");
}

📝 Key Takeaways (الملخص)

  • Arithmetic for Calculation: Use standard math symbols to perform calculations on numbers in your variables.
    • Darija: استعمل علامات الحساب العادية باش تخدم على الأرقام ديالك.
  • Strict Comparison (===): This is the gold standard for checking equality because it checks both value and data type to avoid bugs.
    • Darija: ديما قارن باستعمال === باش تكون متأكد باللي كلشي هو هداك وماكاينش غلط فـ النوع ديال البيانات.
  • Combining Logic: Use && and || to create complex decision-making rules in your applications.
    • Darija: جمع الشروط ديالك بـ AND و OR باش تصاوب قوانين دقيقة فـ البرنامج ديالك.
  • Directing Code Flow: Master if and else statements to guide the user's journey through your website.
    • Darija: تعلم تتحكم فـ المسار ديال الكود على حساب الاختيارات ديال المستخدم.
  • The Safety Net: Always use try...catch when working with risky code (like fetching data) to keep your app running smoothly even when errors happen.
    • Darija: ديما دير "الشبكة ديال الأمان" فـ البلايص اللي ممكن يوقع فيهم غلط باش الموقع ديالك ما يوقفش على الخدمة.

See you in the next part! :)

Related Cheat Sheets

Mentorship

Want to learn, not just read?

These cheat sheets are the short version. If you want the long one, I teach — one on one, at your pace, on what you are actually trying to build.

Let's talk
  • Learn with guidance

    I will walk you through the fundamentals, review your code, and help you grow past the tutorials.

  • Choose your career path

    Not sure which direction to take? We will talk it through and map a path that fits your strengths and your goals.

Want to work together?

I'm always open to discussing new projects.

Get in Touch