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
EasyCreate 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
displayWelcomewith"Alice" - Call
processDatawith no arguments - Call
logErrorwith"Invalid input detected" - Call
displayWelcomewith"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"This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Introduction To TypeScript
1Getting Started with TS
What is TypeScript?Why Use TypeScript?Your First TypeScript CodeCompilation Process & ErrorsRecap: Introduction to TS4Working with Functions
Typing Params & Return ValuesTyping Arrow FunctionsThe 'void' Return TypeOptional Parameters with '?'Default Parameter ValuesTyping Rest ParametersDefining Function TypesRecap: Building Typed Funcs2Core Types
Basic Types: str, num, booleanThe 'any' Type: Escape HatchThe 'unknown' TypeWorking with 'null' & 'undef'Type Inference in ActionExplicit Type AnnotationsRecap: Core Types Practice5Types: Aliases, Unions & Inter
Type Aliases for PrimitivesUnion Types ('|')Working with Union TypesLiteral TypesIntersection Types ('&')Combining Type AliasesRecap: Advanced Type Combos3Data Structure: Arrays & Tuple
Typed Arrays'readonly' Modifier for ArraysWhat is a Tuple?Declaring and Accessing TuplesDestructuring TuplesReadonly TuplesMulti-dimensional Typed Arrays Spread Operator with ArraysRecap: Arrays and Tuples