null Versus undefined
Two different kinds of absence.
null Versus undefined is a free Zig Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Two Words, Two Meanings
Zig has both null and undefined, and they are not the same. Mixing them up is a classic source of confusing bugs.
null Means No Value
null is a real, defined state: it says this optional intentionally holds nothing. It is a meaningful, checkable value.
const x: ?i32 = null;undefined Means Unspecified
undefined means the storage exists but its contents are garbage. You are promising to write a real value before you read it.
var buf: [4]u8 = undefined;Only Optionals Hold null
You can assign null only to optional types. A plain i32 will never accept it, because absence is not part of its type.
Any Type Can Be undefined
By contrast, almost any variable can start as undefined to reserve memory now and fill it in a moment later.
var total: i32 = undefined;Reading null Is Fine
Checking an optional against null is perfectly safe and expected; that is exactly how you detect an absent value.
Reading undefined Is a Bug
Reading a value that is still undefined is illegal behavior. Safe builds even fill it with 0xAA bytes to expose the mistake.
A Promise to Yourself
Think of undefined as a contract: you swear to assign before use. Break it and you read meaningless, unpredictable data.
Common Buffer Pattern
A frequent idiom is declaring a buffer as undefined, then immediately filling it from a read or a format call.
var line: [256]u8 = undefined;Pick the Right Tool
Use null to model genuine optionality. Use undefined only to skip a redundant initialization you will overwrite right away.
Different Questions Entirely
null answers is there a value, while undefined answers has the memory been written yet. Keeping them separate keeps you safe.
Quick Check
You declare var buf: [8]u8 = undefined; and read buf[0] before writing it. What is that?
Recap
You separated null (a real absent value for optionals) from undefined (uninitialized memory you must write first). Two ideas, never confused. 🧠
Frequently asked questions
Is the “null Versus undefined” lesson free?
Yes — the full text of “null Versus undefined” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.
What will I learn in “null Versus undefined”?
Two different kinds of absence. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Zig Academy?
No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “null Versus undefined” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Zig Academy lesson?
Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Declaring Optional Types
- Unwrap Safely with if and orelse
- Optional Pointers and ?*T
- null Versus undefined