Skip to main content

Variables & Constants

One variable is a space in memory where we can store a value. In Dart, the way to create and initialize a variable is as follows:

var name = 'Yayo';

The compiler can infer that the variable name type is String. Read more about type inference. A different way to create and initialize a variable is by writing the type explicitly, like:

String name = 'Yayo';

The value of variables can change like:

String name = 'Yayo';
name = 'Carlos';

If you want to create a variable whose value never changes, we can use final or const. The difference between final and const is that final can be initialized at run time and const has to be initialized at compile time, like:

const name = 'Yayo'; // const because we initialize it at compilation time.

final String lastName; // final because we initialize it at run time
lastName = 'Arellano';

Camel case

When naming the variables, the Dart team recommends using camel case.


This is an example in DartPad that you can try in the browser; just press Run to see the results.