Menu
Coddy logo textTech

The 'void' Return Type

Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 24 of 73.

When a function performs an action but doesn't return anything meaningful, you use the void return type to explicitly indicate this intention.

The void type represents the absence of a return value. Functions that log messages, update variables, or perform side effects typically use void as their return type:

function logMessage(message: string): void {
  console.log(message);
}

function updateCounter(): void {
  counter++;
}

While TypeScript can often infer that a function returns void, explicitly adding the : void annotation makes your code more readable and communicates your intent clearly to other developers.

challenge icon

Challenge

Easy

Create a function named displayWelcome that takes a parameter userName of type string and has an explicit return type of void. The function should print a welcome message to the console in the format: "Welcome to our application, [userName]!"

Create another function named logError that takes a parameter errorMessage of type string and has an explicit return type of void. The function should print an error message to the console in the format: "ERROR: [errorMessage]"

Create a third function named processData that takes no parameters and has an explicit return type of void. The function should print the message "Processing data..." to the console, then print "Data processing complete." on a new line.

Test your functions by calling them in the following order:

  • Call displayWelcome with "Alice"
  • Call processData with no arguments
  • Call logError with "Invalid input detected"
  • Call displayWelcome with "Bob"

Each function call should produce output on separate lines in the order specified above.

Try it yourself

// TODO: Write your code here
// Create the displayWelcome function that takes userName (string) and returns void
// Create the logError function that takes errorMessage (string) and returns void  
// Create the processData function that takes no parameters and returns void

// TODO: Call the functions in the specified order:
// 1. displayWelcome with "Alice"
// 2. processData with no arguments
// 3. logError with "Invalid input detected"
// 4. displayWelcome with "Bob"
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Introduction To TypeScript