null or undefined.null or undefined.user with type { name: string | null; age: number | undefined; } and assign it the value { name: null, age: undefined }.Use | null and | undefined to allow properties to be missing.
isActive and set it to true.Use let isActive: boolean = true; to declare the variable.
displayName that uses a conditional expression to assign user.name if it is not null, otherwise assign the string 'Guest'.Use user.name !== null ? user.name : 'Guest' to safely assign the name.
console.log statement to print the value of displayName.Use console.log(displayName); to show the result.
What does enabling strictNullChecks in TypeScript do?
strictNullChecks makes TypeScript require explicit handling of null and undefined values to avoid runtime errors.null and undefined values. correctly describes this behavior. Options B, C, and D are incorrect because they describe behaviors that strictNullChecks does not do.null and undefined values. -> Option DWhich of the following is the correct way to check for null or undefined before accessing a variable value in TypeScript with strictNullChecks enabled?
let value: string | null | undefined;
strictNullChecks, you must check both null and undefined explicitly before accessing properties.null and undefined. if (value) { console.log(value.length); } may fail if value is an empty string. console.log(value.length); causes an error. if (value == null) { console.log(value.length); } checks for null but then accesses length inside the if, which is incorrect logic.What will be the output of the following TypeScript code with strictNullChecks enabled?
function greet(name: string | null) {
if (name === null) {
return "Hello, guest!";
}
return `Hello, ${name.toUpperCase()}!`;
}
console.log(greet(null));
console.log(greet("alice"));name === null and returns "Hello, guest!" in that case, so it safely handles null.greet(null) returns "Hello, guest!". Calling greet("alice") returns "Hello, ALICE!" because toUpperCase() is called on a string.Identify the error in this TypeScript code with strictNullChecks enabled and suggest a fix:
function printLength(text: string | null) {
console.log(text.length);
}text.length without checking if text is null causes a compile error under strictNullChecks.if (text !== null) before accessing length to ensure safety.length. -> Option AGiven this TypeScript function with strictNullChecks enabled, which modification safely handles undefined and null inputs without errors?
function formatName(name: string | null | undefined) {
return name.toUpperCase();
}toUpperCase() on name without checking for null or undefined causes errors with strictNullChecks.if (name == null) return "Unknown"; before calling toUpperCase(). adds a check for both null and undefined (using == null) and returns a default string, safely avoiding errors. Change parameter type to string only and remove null and undefined. removes null safety but is less flexible. Use non-null assertion: return name!.toUpperCase(); forces non-null assertion which can cause runtime errors. Remove strictNullChecks from tsconfig. disables safety feature, not recommended.if (name == null) return "Unknown"; before calling toUpperCase(). -> Option CPass the test and this pattern moves unknown