Day 39: Nullable Strings

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
Enter fullscreen mode Exit fullscreen mode

Originally published on DEV Community . Read the discussion and share your thoughts there.

© 2026

LinkedIn 𝕏 GitHub