null and undefined Checks
With strictNullChecks, null and undefined are separate types. Use ?. and ?? to handle them.
Example
function len(s?: string | null) {
return s?.length ?? 0;
}
console.log(len("abc"), len(null), len());Result
3 0 0
Exercise
Default value.
s "none"
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.