0
0
Typescriptprogramming~5 mins

Null and undefined types in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the difference between null and undefined in TypeScript?

null means a variable explicitly has no value. undefined means a variable has been declared but not assigned a value yet.

Click to reveal answer
beginner
How do you declare a variable that can hold either a string or null in TypeScript?
<p>Use a union type like <code>let name: string | null;</code> to allow <code>name</code> to be a string or <code>null</code>.</p>
Click to reveal answer
intermediate
What happens if you try to access a property on undefined in TypeScript?

You get a runtime error because undefined has no properties. TypeScript helps catch this with strict null checks.

Click to reveal answer
intermediate
What is the effect of enabling strictNullChecks in TypeScript?

It forces you to explicitly handle null and undefined values, preventing accidental errors by requiring checks or unions.

Click to reveal answer
beginner
How can you safely access a property of an object that might be null or undefined?

Use the optional chaining operator ?., for example: obj?.property. This returns undefined if obj is null or undefined without error.

Click to reveal answer
In TypeScript, what does undefined mean?
AA variable explicitly has no value
BA variable is a number zero
CA variable has been declared but not assigned a value
DA variable is a boolean false
Which TypeScript setting helps catch errors with null and undefined?
AstrictNullChecks
BnoImplicitAny
CesModuleInterop
DallowJs
What does the optional chaining operator ?. do?
AConverts null to undefined
BAccesses a property only if the object is not null or undefined
CThrows an error if the object is null
DAssigns a default value if the property is missing
How do you declare a variable that can be a string or null?
Alet x: string | null;
Blet x: string & null;
Clet x: string?
Dlet x: null | undefined;
What error occurs if you try to access a property on undefined?
ANo error
BSyntax error
CCompile error
DRuntime error
Explain the difference between null and undefined in TypeScript and how to handle them safely.
Think about how variables get these values and how TypeScript helps avoid errors.
You got /4 concepts.
    Describe how enabling strictNullChecks changes the way you write TypeScript code.
    Consider what happens if you don't check for null or undefined.
    You got /4 concepts.