30 TypeScript Basic Exercises for Beginners with Solutions

Master beginner TypeScript skills with our comprehensive list of top 30 exercises. Dive into coding challenges that improve your understanding and proficiency in TypeScript, setting a solid foundation for intermediate challenges. Start your journey to TypeScript mastery today!

Learning Objectives:

Gain a strong grasp of TypeScript fundamentals including types, functions, interfaces, and classes. Develop the ability to write type-safe code and solve basic programming problems using TypeScript.

Exercise Instructions:

  • Start with the first exercise and attempt to solve it before checking the hint or solution.
  • Ensure you understand the logic behind each solution, as this will help you in more complex problems.
  • Use these exercises to reinforce your learning and identify areas that may require further study.

1. Write a TypeScript program to print "Hello, World!" to the console.

Required Input:

None

Expected Output:

Hello, World!

Code In Typescript

// Predefined structure // Your logic here console.log("Hello, World!");

Run Code?

Click Run Button to view compiled output

2. Create a variable of type string, number, and boolean and print their values.

Required Input:

String: "John Doe", Number: 25, Boolean: true

Expected Output:

John Doe
25
true

Code In Typescript

let name: string; // Assign a value let age: number; // Assign a value let isStudent: boolean; // Assign a value // Your logic here

Run Code?

Click Run Button to view compiled output

3. Write a program to add two numbers and display the result.

Required Input:

Number 1: 5, Number 2: 10

Expected Output:

15

Code In Typescript

let num1: number; // Assign a value let num2: number; // Assign a value // Your logic here

Run Code?

Click Run Button to view compiled output

4. Write a program to find the largest number between two numbers.

Required Input:

Number 1: 12, Number 2: 20

Expected Output:

20

Code In Typescript

let num1: number; // Assign a value let num2: number; // Assign a value // Your logic here

Run Code?

Click Run Button to view compiled output

5. Create a function that takes a name as a parameter and returns a greeting message.

Required Input:

Name: "Alice"

Expected Output:

Hello, Alice!

Code In Typescript

function greet(name: string): string { // Your logic here } // Call the function and print the result

Run Code?

Click Run Button to view compiled output

6. Write a program to calculate the area of a rectangle using length and width.

Required Input:

Length: 10, Width: 5

Expected Output:

Area: 50

Code In Typescript

let length: number; // Assign a value let width: number; // Assign a value // Your logic here

Run Code?

Click Run Button to view compiled output

7. Create an array of numbers and print each number using a for loop.

Required Input:

[1, 2, 3, 4, 5]

Expected Output:

1
2
3
4
5

Code In Typescript

let numbers: number[]; // Assign an array of numbers // Your logic here

Run Code?

Click Run Button to view compiled output

8. Write a TypeScript function to check if a number is even or odd.

Required Input:

4

Expected Output:

4 is even

Code In Typescript

function checkEvenOrOdd(num: number): void { // Your logic here } // Call the function

Run Code?

Click Run Button to view compiled output

9. Write a function to reverse a string and return it.

Required Input:

"hello"

Expected Output:

olleh

Code In Typescript

function reverseString(str: string): string { // Your logic here } // Call the function

Run Code?

Click Run Button to view compiled output

10. Create a function to calculate the factorial of a given number.

Required Input:

5

Expected Output:

Factorial: 120

Code In Typescript

function factorial(n: number): number { // Your logic here } // Call the function

Run Code?

Click Run Button to view compiled output

ad vertical

1 of 3