0
0
Typescriptprogramming~5 mins

Void type for functions in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the void type mean when used as a function's return type in TypeScript?
It means the function does not return any value. The function performs actions but does not give back a result.
Click to reveal answer
beginner
How do you declare a function that returns nothing using void in TypeScript?
You write the function with <code>: void</code> after the parentheses. For example: <br><pre>function greet(): void {<br>  console.log('Hello!');<br>}</pre>
Click to reveal answer
intermediate
Can a function with void return type return null or undefined explicitly?
Yes, it can return undefined explicitly but not null. Usually, functions with void return type do not return anything at all.
Click to reveal answer
beginner
What happens if a function declared with void return type tries to return a value?
TypeScript will show an error because the function promises not to return any value, but a value is returned.
Click to reveal answer
beginner
Why is using void useful in TypeScript functions?
It helps to clearly show that the function is only for doing something (like printing or changing data) and not for producing a value. This makes code easier to understand.
Click to reveal answer
What does a function with return type void do?
AReturns nothing
BReturns a value
CReturns a string
DReturns a number
Which of these is a correct way to declare a void function in TypeScript?
Afunction sayHi(): void { console.log('Hi'); }
Bfunction sayHi(): string { console.log('Hi'); }
Cfunction sayHi() { return 'Hi'; }
Dfunction sayHi(): number { return 5; }
What error occurs if a void function tries to return a value?
ANo error
BTypeScript error: Type 'value' is not assignable to type 'void'
CRuntime error
DSyntax error
Can a void function explicitly return undefined?
ANo
BOnly if it returns a string
CYes
DOnly if it returns a number
Why use void as a function return type?
ATo indicate the function returns an object
BTo indicate the function returns a string
CTo indicate the function returns a number
DTo indicate the function returns nothing
Explain what the void type means for a function's return type in TypeScript and why it is useful.
Think about what happens when a function does not give back any result.
You got /3 concepts.
    Describe what happens if you try to return a value from a function declared with void return type.
    Consider TypeScript's type checking rules.
    You got /3 concepts.