var, final, and const Explained
Choose between mutable, run-time, and compile-time values.
var, final, and const Explained is a free Dart Academy lesson on CoddyKit — lesson 1 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 Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Three Ways to Name a Value
In Dart you store data in named boxes called variables. The keyword you pick decides whether that box can ever change.
Meet var
Use var when a value will change later. Dart still figures out the type for you, so var is convenient and safe.
var score = 0;
score = 10;var Locks the Type
Even though var looks loose, the type is fixed once. Assign an int and you cannot later store text in the same box.
var count = 5;
count = 6; // okMeet final
Use final when a value is set once and never reassigned. It is decided while your program runs, then stays put.
final name = "Ada";final Resists Reassignment
Try to give a final a new value and Dart stops you at compile time. This protects values you never meant to change.
final pi = 3.14;
// pi = 3; // error!Meet const
Use const for values known before the program even runs. They are baked in at compile time, fixed forever.
const maxUsers = 100;const Means Compile-Time
A const value must be computable while compiling. You cannot use something only known at run time, like the current clock.
const now = DateTime.now(); // errorfinal vs const in One Line
Think of it this way: final is set once at run time, while const is frozen even earlier, at compile time. 🧊
Prefer final Over var
A good habit is reaching for final first. If a value never needs to change, final makes your intent clear and prevents bugs.
const Objects Are Canonical
Two identical const objects are shared as the same instance in memory. This saves space and is great for fixed config values.
const a = [1, 2];
const b = [1, 2]; // same objectChoosing the Right Keyword
Will it change? Use var. Set once at run time? Use final. Known before running? Use const. That single question guides you.
Quick Check
Pick the keyword for a value computed at run time but never reassigned.
Recap: var, final, const
You learned that var can change, final is set once at run time, and const is frozen at compile time. Reach for final by default. 🎉
Frequently asked questions
Is the “var, final, and const Explained” lesson free?
Yes — the full text of “var, final, and const Explained” is free to read here on the web, and the Dart 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 Dart Academy course, upgrade to CoddyKit PRO.
What will I learn in “var, final, and const Explained”?
Choose between mutable, run-time, and compile-time values. You practise Dart 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 Dart Academy?
No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “var, final, and const Explained” 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 Dart Academy lesson?
Yes. Every Dart 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
- var, final, and const Explained
- The Core Types: int, double, String, bool
- Type Inference vs Explicit Types
- The dynamic and Object Types