Ad space (banner)
Lesson 3 / 20

Variables and types (var and annotations)

This lesson covers the basics of declaring variables in Dart.

You can let Dart infer the type with var, or state it explicitly as String, int, and so on. Once the type is settled, no value of another type can be assigned.

The sample code declares two variables — String name = 'Alice'; and int age = 14; — and builds a message with string interpolation, the '$variableName' notation.

A common early stumble is the interpolation syntax itself. A plain $ is enough to embed a variable, as in 'Hello, $name', but to embed a whole expression such as a calculation you need the dollar-sign-plus-braces form.

In professional work, string interpolation comes up constantly when assembling the text shown in a user interface.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Dart directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)