DEV Community

Cover image for var vs dynamic in Dart

var vs dynamic in Dart

Nitish Kumar Singh on February 21, 2019

In Dart and JavaScript we have var keyword for the variable declaration but both are not same. There is the dynamic keyword for variable...
Collapse
 
dance2die profile image
Sung M. Kim

Thanks for the post, Nitish 👍

I've never used Dart and the post was very easy to follow.
How I understood about var in Dart is that, Dart keeps track of initial variable type while dynamic.

And shouldn't the example 4 use dynamic keyword?

// This is invalid Example this will work
void main() {
     dynamic x = 'Math pi';
     print(x); // Maths pi
     x = 3.14; 
     print(x); // 3.14
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
slaveofcode profile image
Aditya Kresna

Awesome!

Collapse
 
klanmiko profile image
Kaelan Mikowicz

Cool, so var can be either dynamic or another type depending on how it is initialized.

Collapse
 
creativ_bracket profile image
Jermaine

Thanks for sharing Nitish! Keep them coming.

Collapse
 
josefatef profile image
josefatef

Thanks

Collapse
 
ilgeion86 profile image
Vy

Very usefull post for understanding. Thnx !