0
0
Typescriptprogramming~20 mins

Why typed functions matter in Typescript - See It in Action

Choose your learning style9 modes available
Why typed functions matter
📖 Scenario: Imagine you are building a simple calculator app. You want to make sure the functions you write only accept numbers and return numbers. This helps avoid mistakes and makes your code safer and easier to understand.
🎯 Goal: You will create typed functions in TypeScript that add and multiply numbers. You will see how specifying types helps catch errors early and makes your code clearer.
📋 What You'll Learn
Create a function called add that takes two numbers and returns a number
Create a function called multiply that takes two numbers and returns a number
Call both functions with numbers and store the results
Print the results to the console
💡 Why This Matters
🌍 Real World
Typed functions are used in real apps to avoid bugs and make code easier to maintain.
💼 Career
Many companies use TypeScript to write safer JavaScript code, so knowing typed functions is important for developers.
Progress0 / 4 steps
1
Create the add function with typed parameters and return
Write a function called add that takes two parameters named a and b, both of type number, and returns a number. The function should return the sum of a and b.
Typescript
Need a hint?

Use function add(a: number, b: number): number to define the function with typed parameters and return type.

2
Create the multiply function with typed parameters and return
Add a function called multiply that takes two parameters named x and y, both of type number, and returns a number. The function should return the product of x and y.
Typescript
Need a hint?

Define multiply similar to add, but return the product of x and y.

3
Call the functions and store the results
Create two variables called sumResult and productResult. Assign sumResult the result of calling add with 5 and 3. Assign productResult the result of calling multiply with 5 and 3.
Typescript
Need a hint?

Call add(5, 3) and multiply(5, 3) and store results in variables.

4
Print the results to the console
Write two console.log statements to print sumResult and productResult.
Typescript
Need a hint?

Use console.log(sumResult) and console.log(productResult) to show the results.