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
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
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
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
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
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
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
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
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
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
Run Code?
Click Run Button to view compiled output