0
0
Typescriptprogramming~15 mins

Void type for functions in Typescript - Mini Project: Build & Apply

Choose your learning style9 modes available
Void type for functions
📖 Scenario: Imagine you are creating a simple logging system for a website. You want to write functions that perform actions but do not return any value.
🎯 Goal: Learn how to write functions in TypeScript that use the void type to indicate they do not return any value.
📋 What You'll Learn
Create a function with void return type
Call the function to perform an action
Use console.log inside the function to show a message
💡 Why This Matters
🌍 Real World
Functions that perform actions like logging, updating UI, or sending data often do not return values. Using <code>void</code> clearly shows this intent.
💼 Career
Understanding <code>void</code> functions is important for writing clear and correct TypeScript code in web development and software engineering.
Progress0 / 4 steps
1
Create a function with void return type
Write a function called logMessage that takes a single parameter message of type string and has a return type of void. Inside the function, use console.log(message) to print the message.
Typescript
Need a hint?

Use function keyword, specify message: string as parameter, and : void after the parameter list.

2
Add a message to log
Create a variable called myMessage and assign it the string value "Hello, TypeScript!".
Typescript
Need a hint?

Use const myMessage: string = "Hello, TypeScript!"; to create the variable.

3
Call the function with the message
Call the function logMessage and pass the variable myMessage as the argument.
Typescript
Need a hint?

Use logMessage(myMessage); to call the function.

4
Display the output
Run the program and observe the output printed to the console. It should display the message Hello, TypeScript!.
Typescript
Need a hint?

Check the console output after running the program.