null / undefined null and undefined

Last updated: 2026-10-22

null / undefined null and undefined

null means "intentionally empty"; undefined means "not assigned / missing". With strictNullChecks they are standalone types and must be handled before normal use.

GroupBasic Types
KindType
Strict modeRequired

ℹ️ Info The editor transpiles TypeScript to JavaScript and runs it (no type checking); see each example's Output for type-check results.

📝 Syntax

let nickname: string | null = null;
if (nickname !== null) {
  nickname.toUpperCase(); // usable only after excluding null
}
let maybe = list.find(x => x.ok); // undefined means not found

⚙️ Parameters

a?.bShort-circuits to undefined when the left side is null/undefined, without throwing
a ?? bTakes the right side when the left is null/undefined; 0 and "" are not skipped

Returns:None — null/undefined are basic type annotations and produce no runtime code.

▶ Example

Edit the code below and click Try it to see the transpiled result:

Output:

Ada guest
0

(Runtime output after transpilation; with strictNullChecks on, calling missing.length directly is a compile error)

💡 Notes

⚠️ Note With strictNullChecks off, null/undefined assign to anything and null-pointer safety rests on luck. Always enable strict in new projects.
💡 Tip ?. and ?? are the golden pair: user?.address?.city ?? "unknown" reads and defaults in one line.
ℹ️ Info ?? falls through only for null/undefined, preserving 0, false and "" — the crucial difference from ||.
More cases coming soon.

📚 Related tutorials

❓ FAQ

Q ?? vs ||?
A || treats every falsy value (0, "", false) as empty; ?? only null/undefined. Defaulting should almost always use ?? so 0 and "" are not clobbered.
Q How should null and undefined be used semantically?
A Convention: undefined = no value yet (missing argument/property); null = deliberately emptied. Be consistent within one API.
Q What type does optional chaining produce?
A T | undefined: if any link can be empty, the result carries undefined — follow with ?? or narrow.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏