1 min read
❤️ 0 · 💬 1
#javascript
#typescript
#100daysofcode
#day39
Question: Handling Nullable Strings
Write a function where you need to calculate the length of the string if it's present, or return -1 if it's null. TypeScript's strict type checking can make this seemingly simple task a bit tricky.
const str1: string | null = "Hello, TypeScript!";
const str2: string | null = null;
console.log(getStringLength(str1)); // Output: 18
console.log(getStringLength(str2)); // Output: -1
Originally published on DEV Community . Read the discussion and share your thoughts there.