Skip to content
Home » Introduction to TypeScript: Enhancing JavaScript Development » Module 3: Functions and Control Flow – Orchestrating Your TypeScript Code

Module 3: Functions and Control Flow – Orchestrating Your TypeScript Code

Module 3 dives into the world of functions and control flow statements, empowering you to create reusable blocks of code and control the execution flow of your TypeScript program based on specific conditions.

3.1 Functions in TypeScript:

  • Defining Functions:
    • Utilize the function keyword followed by the function name, parentheses for arguments (optional), and curly braces {} to define the code block.
    • Declare types for function parameters and return values using type annotations (e.g., function greet(name: string): string { ... }).
    • Example:

TypeScript

function greet(name: string): string {
  return "Hello, " + name + "!";
}

let message = greet("Alice");
console.log(message); // Output: Hello, Alice!
  • Function Expressions:
    • Offer an alternative way to define functions using a concise syntax:

TypeScript

const greet = (name: string): string => {
  return "Hello, " + name + "!";
};
  • Arrow Functions (Optional):
    • Introduced in ES6 (ECMAScript 2015), arrow functions provide a shorter syntax for defining functions, particularly useful for simple expressions:

TypeScript

const greet = (name: string) => "Hello, " + name + "!";

3.2 Control Flow Statements:

  • if statements: Execute a block of code if a certain condition is true:

TypeScript

let age = 25;

if (age >= 18) {
  console.log("You are eligible to vote.");
} else {
  console.log("You are not eligible to vote.");
}
  • else if statements: Check multiple conditions sequentially:

TypeScript

let grade = 85;

if (grade >= 90) {
  console.log("Excellent!");
} else if (grade >= 80) {
  console.log("Very good!");
} else {
  console.log("Good effort.");
}
  • switch statements: Provide a more concise way to handle multiple conditions related to the same variable:

TypeScript

let day = "Tuesday";

switch (day) {
  case "Monday":
  case "Tuesday":
  case "Wednesday":
  case "Thursday":
  case "Friday":
    console.log("It's a weekday.");
    break;
  case "Saturday":
  case "Sunday":
    console.log("It's a weekend.");
    break;
  default:
    console.log("Invalid day.");
}
  • for and while loops: Repeat a block of code multiple times based on a condition:

TypeScript

// for loop
for (let i = 0; i < 5; i++) {
  console.log("Iteration:", i);
}

// while loop
let count = 0;
while (count < 3) {
  console.log("Count:", count);
  count++;
}
  • break and continue statements: Control the flow within loops:
    • break: Exit the loop prematurely when a condition is met.
    • continue: Skip the remaining code in the current iteration and jump to the next iteration.

3.3 Error Handling:

  • try...catch blocks: Handle runtime errors gracefully:

TypeScript

try {
  let result = 10 / 0; // This will cause an error
  console.log(result);
} catch (error) {
  console.log("Error:", error.message);
}

3.4 Practice and Challenges:

  • Engage in coding exercises to practice defining and calling functions with various arguments and return types.
  • Experiment with different control flow statements to construct various program logic scenarios.
  • Solve coding challenges that involve making decisions and repeating code based on conditions, utilizing error handling mechanisms to ensure robustness.

3.5 Conclusion:

By mastering functions and control flow statements in TypeScript, you empower your programs to make decisions, repeat code efficiently, and handle different scenarios dynamically. This module equips you with the tools to write well-structured and maintainable code, allowing you to build more complex and interactive applications.


Home » Introduction to TypeScript: Enhancing JavaScript Development » Module 3: Functions and Control Flow – Orchestrating Your TypeScript Code