Skip to main content

Typescript

Published 1 min readMarkdown
Typescript

Optional Parameters

Use a question mark (?) for optional parameters.

function greetWithTitle(title: string, name?: string): string {
   return `Hello, ${title}${name ? ' ' + name : ''}!`;
}

Arrow Functions

Arrow functions are concise. For a single statement, you can omit curly braces:

const add = (a: number, b: number): number => a + b;

Array Methods: forEach, map, and filter

The forEach Method

Iterate over array elements easily.

let fruits: string[] = ["banana", "mango", "apple", "cherry"];
fruits.forEach((fruit) => console.log(fruit));

The map Method

Create a new array by applying a function to each element.

const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map((num) => num * 2);

console.log(doubled); // Output: [2, 4, 6, 8, 10]

The filter Method

Create a new array containing elements that pass a test.

const ages = [15, 20, 17, 30, 14];
const adults = ages.filter((age) => age >= 18);

console.log(adults); // Output: [20, 30]

About the author

  • Hello, I'm Sheheryar Altaf, a BSCS student at COMSATS University Islamabad. Passionate about computer science, I'm dedicated to mastering Object-Oriented Programming (OOP), Data Structures and Algorithms (DSA), and databases.

    I have practical experience in projects like the Voting Management System database, where I've applied these skills hands-on.

    I'm eager to tackle new challenges and continuously expand my knowledge and skills in computer science. Here's to excelling in my studies and embracing endless possibilities!

No comments yet. Be the first to comment.

More from this blog